Encountering the message TypeError: can only concatenate str not int to str is one of the most common experiences for anyone learning Python programming. This leads to this error appears when you try to join, or concatenate, a text string with a whole number without first converting the number into text. Now, because Python is a strongly typed language, it does not automatically change an integer into a string just because you placed a plus sign between them. Understanding why this happens and how to fix it is an essential milestone for beginner developers who want to write clean, error-free code.
What Does "TypeError: can only concatenate str not int to str" Mean?
The error message itself tells you exactly what went wrong, even if it sounds technical at first glance. Let’s break it down:
- TypeError: This category of error means you tried to perform an operation on a data type that does not support it.
- can only concatenate str: Python is telling you that the plus sign (
+) used for joining text only works when both sides are strings. - not int to str: You attempted to attach an integer (a whole number) to a string, and Python refused.
In simpler terms, Python will happily let you add two strings together, such as "Hello" + " World", but it will not let you add a string and an integer directly, such as "Hello" + 5. The language expects both sides of the plus sign to match in type The details matter here. Took long enough..
Why Does This Error Happen?
Python uses dynamic typing, which means you do not have to declare whether a variable is a number or text when you create it. On the flip side, Python is also strongly typed, meaning it does not silently convert one type into another behind the scenes.
When you use the + operator, Python looks at the data types involved:
- If both values are integers, Python performs mathematical addition.
- If both values are strings, Python performs string concatenation.
- If one value is a string and the other is an integer, Python does not know whether you want mathematical addition or text joining, so it raises the TypeError: can only concatenate str not int to str.
This design prevents subtle bugs. Here's one way to look at it: if Python automatically converted the number 5 into the text "5", then "Price: " + 5 might become `"Price:
…"Price: 5", then code like "Total: " + 10 + 5 might confusingly produce "Total: 15" instead of raising an error. By requiring you to convert types explicitly, Python forces you to decide exactly how the output should look That's the whole idea..
How to Fix the Error
The solution is to convert the integer into a string before joining it. Here are the most common ways to do that, from the most explicit to the most modern.
1. Use str()
The simplest fix is wrapping the number with the built-in str() function:
age = 25
print("I am " + str(age) + " years old.")
This converts 25 into "25", so both sides of the + operator are strings and the concatenation succeeds.
2. Use f-strings (Recommended)
Introduced in Python 3.6, f-strings are the most readable and efficient method for combining text and variables:
age = 25
print(f"I am {age} years old.")
Python automatically converts age to a string inside the curly braces, and you avoid cluttering your code with repeated str() calls and plus signs Most people skip this — try not to..
3. Use .format()
Older codebases often use the .format() method:
age = 25
print("I am {} years old.".format(age))
This works well, though it is slightly more verbose than f-strings.
4. Use the % operator (Legacy)
You may encounter this older style in legacy projects:
age = 25
print("I am %d years old." % age)
It is still functional, but modern Python generally favors f-strings or .format() for clarity The details matter here..
A Practical Example
Imagine you are building a receipt generator:
item = "Notebook"
price = 12
quantity = 3
# This will raise TypeError: can only concatenate str not int to str
# print("Item: " + item + ", Price: " + price + ", Qty: " + quantity)
# Fixed with f-strings:
print(f"Item: {item}, Price: {price}, Qty: {quantity}")
By using an f-string, you keep the logic clean and avoid type mismatches entirely But it adds up..
Watch Out for Other Types
The same TypeError appears with other non-string types, such as floats, None, or lists:
# "Score: " + 98.6 # TypeError with float
# "Result: " + None # TypeError with NoneType
The fix is identical: convert the value with str() or interpolate it with an f-string.
Conclusion
The error TypeError: can only concatenate str not int to str is Python’s way of protecting you from ambiguous behavior. Because Python is strongly typed, it refuses to guess whether you want mathematical addition or text joining when the types don’t match. Here's the thing — the fix is straightforward: explicitly convert numbers to strings using str(), or—better yet—use f-strings to embed variables directly into your text. Once you make this distinction a habit, you’ll write clearer code and spend less time debugging avoidable type mismatches.