Type Conversion
A free lesson from Swipe Coding · Python, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
Type conversion means turning a value of one type into another, and in Python you always do it explicitly by calling a function: int(), float(), or str(). Nothing converts automatically behind your back — you have to ask for it.
age_text = "25" age_number = int(age_text) print(age_number + 1) # 26 print(type(age_number)) # <class 'int'>
float() works the same way but produces a decimal number, and str() goes the opposite direction, turning any value — a number, a boolean, anything — into its text representation.
price_text = "4.99" price = float(price_text) print(price * 2) # 9.98 count = 5 message = "You have " + str(count) + " items" print(message)
Converting between numeric types has one important trap: int() cannot directly parse a string that contains a decimal point. Calling int("3.5") raises a ValueError, because int() expects a string that looks like a whole number. To handle text like "3.5", you must convert it to a float first, and then, if you really want a whole number, convert that float to an int — which truncates (cuts off) the decimal part rather than rounding.
# int("3.5") # raises ValueError: invalid literal for int()
value = float("3.5") # 3.5, works fine
whole = int(value) # 3, truncated — not rounded
print(whole)Calling int("3.5") directly raises a ____, because int() cannot parse a decimal point in text.
What is the correct way to turn the text "3.5" into the int 3?
str() can convert values of any type, including numbers and booleans, into their text representation.
Booleans & Type Conversion
10 questions · pass with 60% — just like in the app.
How are the boolean values written in Python?
Comparison operators like == and >= always produce a bool result.
The number ____ is considered falsy in Python.
Which of these values is falsy?
The value None is falsy in Python.
The ____ function converts a string like "25" into an actual whole number.
What happens when you call int("3.5") directly?
Converting "3.5" to an int works if you convert it to a float first, then to an int.
The ____ function converts a value like 42 or True into its text representation.
What does int(float("3.5")) evaluate to?
Keep your streak going in Swipe Coding
Swipe through every level of Python on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →