TypeError: can only concatenate str (not "int") to str
Youβre trying to combine a string with a number using +. Python doesnβt auto-convert types.
Fix 1: Convert to string with str()
# β Can't add string + int
age = 25
message = "I am " + age + " years old" # TypeError!
# β
Convert the number to string
message = "I am " + str(age) + " years old"
Fix 2: Use f-strings (best approach)
age = 25
name = "Alice"
# β
f-strings handle conversion automatically
message = f"My name is {name} and I am {age} years old"
F-strings are the modern Python way. Theyβre cleaner and faster than concatenation.
Fix 3: Use .format()
# β
Also works
message = "I am {} years old".format(age)
message = "I am {age} years old".format(age=25)
Fix 4: Use % formatting (older style)
# β
Still works, but f-strings are preferred
message = "I am %d years old" % age
The same error with other types
# β Lists, booleans, floats β same problem
items = ["a", "b"]
print("Items: " + items) # TypeError with list
print("Score: " + 9.5) # TypeError with float
print("Active: " + True) # TypeError with bool
# β
Convert everything
print("Items: " + str(items))
print(f"Score: {9.5}")
print(f"Active: {True}")
Related resources
Related: Python TypeError: βintβ Object Is Not Iterable β How to Fix It
Related: Python TypeError: βstrβ Object Is Not Callable β How to Fix It