πŸ”§ Error Fixes
Β· 1 min read

Python TypeError: Can Only Concatenate Str to Str β€” How to Fix It


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: Python TypeError: β€˜int’ Object Is Not Iterable β€” How to Fix It

Related: Python TypeError: β€˜str’ Object Is Not Callable β€” How to Fix It

πŸ“˜