Start learning free

LESSON 14 OF 673 · VARIABLES

Type Conversion in Python

Lesson

In Python, every value has a type: int, float, str, and so on. Sometimes you need to convert a value from one type to another. This is called type conversion.

Use these built-in functions to convert between types:

int(x)    # converts x to an integer
float(x)  # converts x to a float
str(x)    # converts x to a string

Here are some examples:

score = 42
score_str = str(score)        # "42"  (integer → string)

weight = 70.8
weight_int = int(weight)      # 70    (float → integer, decimal dropped)

count = "100"
count_int = int(count)        # 100   (string → integer)

Note that converting a float to int simply drops the decimal - it does not round.

int(9.99)  # → 9
int(3.1)   # → 3

Assignment
Follow the Coding Tutorial and convert each variable to a different type!


Hint
Use str(), int(), and float().

STEP 1 OF 3
    Ctrl + Enter to check