How to Resolve "TypeError: must be str, not int" in Python
When working in Python, you might encounter a TypeError: must be str, not int or the very similar TypeError: can only concatenate str (not "int") to str. Both errors occur when you try to perform an operation that exclusively requires a string, but you provide an integer instead. The most common cause is attempting to join a string and an integer using the + operator.
Because Python is a strongly-typed language, it does not automatically convert the integer into a string for you in this context. This guide will explain why this error happens and walk you through the standard and modern ways to fix it, with a strong recommendation for using f-strings.
Understanding the Error: Strong Typing and Concatenation
In Python, the + operator has two different meanings depending on the context:
- For numbers, it performs addition.
- For strings, it performs concatenation (joining them together).
When you try to use + with a mix of a string and an integer, Python doesn't know which operation you intend. Should it try to add them (which is nonsensical) or join them? Rather than guessing, it raises a TypeError. You must be explicit about your intent by converting the integer to a string first.
Reproducing the TypeError
The error is most commonly triggered when trying to build a string by concatenating a string literal with an integer variable.
Example of code causing the error:
temp_f = 42
# Incorrect: Trying to concatenate a string with an integer.
message = "Today's temperature is: " + temp_f + " F"
print(message)
Output:
Traceback (most recent call last):
File "main.py", line 4, in <module>
message = "Today's temperature is: " + temp_f + " F"
TypeError: can only concatenate str (not "int") to str
While the error message here is can only concatenate str (not "int") to str, other operations like writing to a file (file.write(42)) will produce the must be str, not int message. The solution for both is the same: convert the integer to a string.
Solution 1: Explicit Conversion with str()
The most direct fix is to wrap your integer variable with the built-in str() function. This explicitly converts the integer to its string representation before the concatenation occurs.
Solution:
temp_f = 42
# ✅ Correct: Convert the integer to a string before concatenating.
message = "Today's temperature is: " + str(temp_f) + " F"
print(message)
Output:
Today's temperature is: 42 F
Solution 2: Using f-Strings (Recommended Best Practice)
Introduced in Python 3.6, f-strings (formatted string literals) are the modern, clean, and highly readable way to embed expressions inside string literals. Python handles the conversion for you automatically and efficiently.
Solution:
temp_f = 42
# ✅ Correct: Use an f-string to embed the variable directly.
# The 'f' prefix tells Python to format the string.
message = f"Today's temperature is: {temp_f} F"
print(message)
Output:
Today's temperature is: 42 F
This is the preferred method in modern Python for its clarity and performance.
Solution 3: Using the str.format() Method
Before f-strings, the .format() method was the standard way to format strings. It is still widely used and is more powerful than simple + concatenation.
Solution:
temp_f = 42
# ✅ Correct: Use the .format() method with a placeholder {}.
message = "Today's temperature is: {} F".format(temp_f)
print(message)
Output:
Today's temperature is: 42 F
A Note on the print() Function
You might have noticed that you can pass integers and strings to the print() function separated by commas without an error.
temp_f = 42
print("Today's temperature is:", temp_f, "F")
Output:
Today's temperature is: 42 F
This works because you are passing multiple, separate arguments to print(). The print() function is designed to automatically convert each argument to a string and join them with a space by default. This is different from creating a single string through concatenation with + before passing it to print.
Conclusion
| If you need to... | The best solution is... | Example |
|---|---|---|
| Combine strings and numbers (Modern Python 3.6+) | Use an f-string. | f"Value: {my_int}" |
| Combine strings and numbers (Older Python) | Use the str.format() method. | "Value: {}".format(my_int) |
| Perform a quick fix for concatenation | Explicitly convert with str(). | "Value: " + str(my_int) |
The TypeError: must be str, not int is a fundamental error related to Python's strong typing system. By using modern string formatting tools like f-strings, you can write code that is not only correct but also significantly more readable and maintainable.