Type Conversion (Casting)
Learn how to convert values between types like int, float, str, and bool so Python can understand what you really mean.
Imagine you ask someone their age and they say "twenty-five" — but you need the number 25 to do math with it. That mismatch happens in Python all the time. Every value in Python has a type (like a number, text, or true/false), and sometimes you need to change a value from one type to another. That process is called type conversion (or casting). It is one of the most practical skills in Python — you will use it constantly.
See it in action
— step through the idea, then dive into the details below.Values Have Types — And You Can Switch
Every value in Python has a type — int, str, float, and more. Type conversion (casting) lets you change a value from one type to another so Python understands what you really mean.
#Why Does This Even Come Up?
The most common situation: when you ask a user to type something with input(), Python always gives you back a str (text) — even if they typed a number. So "5" and 5 look the same to you, but Python treats them very differently. You cannot do math on the text "5".
age = input("How old are you? ") # user types: 25
print(type(age))
print(age + 1)The Vending Machine Analogy
Think of type conversion like feeding a vending machine. The machine only accepts coins, not paper bills. If you have a $1 bill (a string "1"), you need to convert it to coins (the integer 1) before the machine will accept it. Python's conversion functions are your coin-change machine.
#Converting to an Integer with int()
Use int() to turn a value into a whole number. This is the fix for the input() problem above. Wrap the whole input() call in int() and Python converts the result right away.
age = int(input("How old are you? ")) # user types: 25
print(type(age))
print(age + 1)int() also works on floats — it simply chops off the decimal part (it does not round):
int(3.9)gives3int(-2.7)gives-2
print(int(3.9))
print(int(-2.7))
print(int("42"))#Converting to a Float with float()
Use float() when you need a number with a decimal point — for example, prices or measurements.
price = float(input("Enter price: ")) # user types: 9.99
tax = price * 0.1
print("Tax:", tax)#Converting to a String with str()
Use str() to turn a number (or anything else) into text. This is super handy when you want to build a message by joining numbers and words together.
score = 98
message = "Your score is " + str(score) + " out of 100!"
print(message)f-strings handle this automatically
Using an f-string (covered in another lesson) lets you skip str() entirely: f"Your score is {score} out of 100!" works great. But understanding str() helps you know what is happening under the hood.
#Converting to a Boolean with bool()
Use bool() to convert a value to True or False. Python has a simple rule: most things are truthy, but a handful of values are falsy:
- Falsy:
0,0.0,""(empty string),[](empty list),None - Everything else is truthy
print(bool(0))
print(bool(42))
print(bool(""))
print(bool("hello"))
print(bool([]))#Converting to a List with list()
Use list() to convert other sequence types into a list. A common use is splitting a string into individual characters.
letters = list("hello")
print(letters)#Implicit Conversion: When Python Helps Automatically
Sometimes Python converts types automatically without you asking. This is called implicit conversion. The most common example: when you add an int and a float, Python quietly upgrades the result to a float so no information is lost.
result = 3 + 1.5
print(result)
print(type(result))Implicit vs. Explicit
Implicit conversion happens automatically (Python decides). Explicit conversion is when you call int(), float(), str(), etc. As a beginner, focus on explicit conversion — it is what you will use most often.
#When Conversion Goes Wrong: ValueError
Python can only convert values that make sense. If you try to turn the word "hello" into an integer, Python has no idea what number that should be — so it raises a ValueError.
number = int("hello")Watch out for spaces and letters in user input
If a user types " 5 " (with spaces) instead of "5", int() actually handles it fine — it trims whitespace. But if they type "five" or "5.0" when you use int(), you will get a ValueError. Use float() if the user might type a decimal, and consider checking their input before converting.
What does this code print? ```python x = input("Enter a number: ") # user types: 7 print(x + x) ```
Key takeaways
- input() always returns a string — use int() or float() to do math with user input.
- Use str() to turn numbers into text when building messages.
- int() truncates decimals; it does not round — int(3.9) is 3, not 4.
- Trying to convert an invalid string like int("hello") raises a ValueError.
- Python implicitly converts int to float when you mix them in math — the result is always a float.
What does this code print?
x = "7"
print(x + x)This code is supposed to add 5 to the user's input and print the result, but it raises a TypeError. What is wrong?
number = input("Enter a number: ")
print(number + 5)Complete the code so it converts the score to a string and prints: "You scored 95 points!"
score = 95 message = "You scored " + (score) + " points!" print(message)
What does this code print?
print(int(3.9))
print(bool(""))
print(bool(42))Write a program that asks the user for two numbers (on separate lines), adds them together as real numbers, and prints a message like: "The sum of 3 and 4.5 is 7.5". Make sure it works even when one number has a decimal point.
Try it live — edit the code and hit Run to execute real Python: