Type Conversion
Learn how to convert values between different types in Python using built-in functions.
Type Conversion
Sometimes you have a value of one type but you need it in another. A number stored as a string that you want to do math with. A float you want to round down to an integer. Python gives you built-in functions to convert between types — this is called type conversion.
There are two kinds:
- Explicit conversion — you convert it yourself using a function like
int(),str(),float() - Implicit conversion — Python converts it automatically behind the scenes
Explicit conversion
int() — convert to integer
Converts a value to a whole number.
# string to int
age = int("22")
print(age) # 22
print(type(age)) # <class 'int'>
# float to int — drops the decimal, does NOT round
x = int(9.99)
print(x) # 9
# bool to int
print(int(True)) # 1
print(int(False)) # 0int() drops the decimal — it does not round. int(9.99) gives you 9, not 10. If you want rounding, use round() instead.
You cannot convert a string that contains a decimal or letters:
int("3.14") # ValueError — use float() first
int("hello") # ValueError — cannot convert text to a numberfloat() — convert to float
Converts a value to a decimal number.
# string to float
price = float("9.99")
print(price) # 9.99
print(type(price)) # <class 'float'>
# int to float
x = float(5)
print(x) # 5.0
# bool to float
print(float(True)) # 1.0
print(float(False)) # 0.0Unlike int(), float() can handle strings with decimal points:
print(float("3.14")) # 3.14
print(float("10")) # 10.0 — works toostr() — convert to string
Converts any value to a string. This one almost never fails.
age = 22
price = 9.99
is_active = True
print(str(age)) # "22"
print(str(price)) # "9.99"
print(str(is_active)) # "True"
print(str(None)) # "None"You will use str() most often when you want to join a number into a string using +:
age = 22
# This breaks — cannot add int to str
print("I am " + age) # TypeError
# This works — convert first
print("I am " + str(age)) # I am 22In practice, f-strings are cleaner than using str() with concatenation. f"I am {age}" is easier to read than "I am " + str(age). Use str() when you specifically need a string variable, not just for printing.
bool() — convert to boolean
Converts a value to True or False. This one is important to understand well because Python uses it constantly in conditions.
print(bool(1)) # True
print(bool(0)) # False
print(bool("hello")) # True
print(bool("")) # False
print(bool([1, 2])) # True
print(bool([])) # False
print(bool(None)) # FalseThe rule is simple — these values are always False:
| Value | Type |
|---|---|
0 | int |
0.0 | float |
"" | empty string |
[] | empty list |
{} | empty dict |
() | empty tuple |
None | NoneType |
Everything else is True. These are called falsy and truthy values, and Python uses them in if statements automatically:
name = ""
if name:
print(f"Hello, {name}!")
else:
print("No name provided.")
# No name provided.Python called bool(name) behind the scenes. Because name is an empty string, it is falsy — so the else branch ran.
Implicit conversion
Python sometimes converts types automatically when it makes obvious sense — you do not ask for it, it just happens.
# int + float — Python promotes int to float automatically
result = 5 + 2.0
print(result) # 7.0
print(type(result)) # <class 'float'>Python will not do implicit conversion when it could cause confusion or data loss:
# str + int — Python refuses, raises TypeError
print("Age: " + 25) # TypeError: can only concatenate str (not "int") to strThis is intentional. Python does not guess — if there is ambiguity, it raises an error and makes you be explicit.
Conversion between types — what works
Not every conversion is valid. Here is a clear map:
| From | To int | To float | To str | To bool |
|---|---|---|---|---|
int | — | ✅ | ✅ | ✅ |
float | ✅ (drops decimal) | — | ✅ | ✅ |
str | ✅ (if numeric) | ✅ (if numeric) | — | ✅ |
bool | ✅ | ✅ | ✅ | — |
None | ❌ | ❌ | ✅ | ✅ |
A practical example
A common real-world pattern — getting a number from input() and doing math with it:
raw = input("Enter a price: ") # always returns str
price = float(raw) # convert to float
discounted = price * 0.9 # apply 10% discount
print(f"Original: {price}")
print(f"Discounted: {discounted:.2f}")Output:
Enter a price: 50
Original: 50.0
Discounted: 45.00Summary
| Function | Converts to | Fails when |
|---|---|---|
int(x) | Whole number | String has decimals or letters |
float(x) | Decimal number | String has letters |
str(x) | String | Almost never |
bool(x) | True or False | Almost never |