🔧 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}")
📘