Python BasicsBeginner7 min11 / 63

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

Visual walkthrough1 / 5
1

Values Have Types — And You Can Switch

Every value in Python has a typeint, str, float, and more. Type conversion (casting) lets you change a value from one type to another so Python understands what you really mean.

You'll use this constantly — especially when dealing with user input.

#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".

input() always returns a string — even when the user types a number
age = input("How old are you? ")  # user types: 25
print(type(age))
print(age + 1)
Think of it like

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.

Wrapping input() with int() converts the text to a number immediately
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) gives 3
  • int(-2.7) gives -2
int() truncates floats and converts numeric strings
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.

float() lets you do decimal math on user input
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.

str() lets you glue numbers into text with the + operator
score = 98
message = "Your score is " + str(score) + " out of 100!"
print(message)
Tip

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
bool() shows you what Python considers True or False
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.

list() breaks a string into a list of 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.

Python automatically makes the result a float when mixing int and float
result = 3 + 1.5
print(result)
print(type(result))
Note

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.

int() raises a ValueError if the string cannot be converted
number = int("hello")
Common mistake

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.

Quick check

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.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
x = "7"
print(x + x)
Fix the bug#2

This code is supposed to add 5 to the user's input and print the result, but it raises a TypeError. What is wrong?

fix-bug
number = input("Enter a number: ")
print(number + 5)
Fill in the blank#3

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)
Predict the output#4

What does this code print?

predict-output
print(int(3.9))
print(bool(""))
print(bool(42))
Your turn
Practice exercise

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:

solution.py · editable