Sometimes you need to convert one data type to another.
# String to Integer
age = int("25") # 25
# String to Float
price = float("19.99") # 19.99
# Number to String
text = str(100) # "100"
# To Boolean
bool(1) # True
bool(0) # False
bool("") # False
bool("hi") # True
Use
type() to see what type a value is:
print(type(25)) # <class 'int'>
print(type("Hello")) # <class 'str'>
print(type(3.14)) # <class 'float'>
# This causes an error:
age = input("Enter age: ") # Returns a string!
new_age = age + 1 # Error! Can't add string + int
# Fix with conversion:
age = int(input("Enter age: "))
new_age = age + 1 # Works!
Interactive Visualization