
What is the ‘TypeError: can only concatenate str (not “int”) to str’ Error?
This TypeError is a very common error in Python that occurs when you try to concatenate a string with a non-string data type (e.g., an integer, a list) using the + operator.
Python performs strong type checking, so it does not allow implicit concatenation of different types.
The error message clearly states, “You can only concatenate a string with another string (not an ‘integer’ type).”
Main Cause
The cause of this error is singular: attempting to directly append a variable or value of a different type to a string using the + operator.
name = "User"
age = 25
# Error: Directly concatenating a string (str) and an integer (int)
message = "Hello, " + name + ". Your age is " + age + "."
# TypeError: can only concatenate str (not "int") to str
In the code above, the age variable is an integer (int) type.
Python cannot directly add the integer 25 to the string "Hello, User. Your age is ", so it raises a TypeError.
How to Fix It
There are several simple and effective ways to solve this problem.
1. Explicit Type Conversion with str()
The most basic solution is to explicitly convert the non-string data to a string using the str() function.
name = "User"
age = 25
# Convert the integer age to a string using str()
message = "Hello, " + name + ". Your age is " + str(age) + "."
print(message)
# Output: Hello, User. Your age is 25.
str(age) converts the integer 25 to the string "25", so all elements become strings and can be concatenated correctly.
2. Using f-strings (Formatted String Literals)
If you are using Python 3.6 or higher, f-strings are the most modern and recommended method.
By prefixing the string with an f and placing variables directly inside curly braces {}, they are automatically converted to strings, which is very convenient.
name = "User"
age = 25
# Conc cisely format the string using an f-string
message = f"Hello, {name}. Your age is {age}."
print(message)
# Output: Hello, {name}. Your age is {age}.
The code becomes much more concise and readable.
3. Using the str.format() Method
Before the introduction of f-strings, the str.format() method was commonly used.
This method involves placing {} placeholders in the string and passing the variables as arguments to the .format() method.
name = "User"
age = 25
# Using the str.format() method
message = "Hello, {}. Your age is {}.".format(name, age)
print(message)
# Output: Hello, User. Your age is 25.
4. Using a Comma in the print Function
If the goal is simply to print multiple values to the console, you can pass them separated by commas in the print function.
The print function automatically separates each argument with a space and prints them.
name = "User"
age = 25
# Using a comma in the print function automatically converts each type to a string for output
print("Hello,", name, ". Your age is", age, ".")
# Output: Hello, User . Your age is 25 .
However, this method is useful only for printing multiple values, not for creating a single string that includes the variables.
Conclusion
TypeError: can only concatenate str (not "int") to str occurs when you try to concatenate non-string data without converting it to a string.
To solve this problem, it is best to either directly convert the type with the str() function or use string formatting features like f-strings or str.format().
F-strings, in particular, are the most concise and efficient method in modern Python, so it is highly recommended to use them actively.
Professional Depth Check
For How to Fix Python TypeError: can only concatenate str (not ‘int’) to str, the practical standard is not whether the reader can repeat one instruction once. Treat the topic as a reproducible debugging procedure: verify interpreter path, virtual environment, package version, and input file or data boundary before drawing a conclusion. The result should be written as a small decision record, because future readers need to know which fact was observed, which assumption was used, and which condition would change the answer.
Evidence That Makes the Guidance Reliable
Use objective evidence before changing a workflow. Good evidence includes python --version, python -m pip show, the full traceback, and a minimal script. If two pieces of evidence conflict, keep the conflict visible instead of smoothing it over. For example, a successful quick fix is still weak evidence if the same input, account, dependency, or device state has not been tested again. A durable article should help the reader distinguish a confirmed fix from a plausible fix.
Review Table
| Review Item | What To Confirm | Why It Matters |
|---|---|---|
| Scope | The exact case covered by this article | Prevents over-applying the advice |
| Baseline | The state before any change | Makes rollback and comparison possible |
| Change | The smallest action taken | Reduces hidden side effects |
| Result | The observed output after the change | Separates evidence from expectation |
| Recheck | When to revisit the conclusion | Keeps the post accurate over time |
Edge Cases and Failure Modes
The main risks are fixing the symptom while leaving the root cause, and mixing unrelated changes into the same test. When the situation involves production data, personal information, money, health, legal rights, or security recovery, the conservative path is to stop and collect evidence before applying a broad fix. The same title can describe very different cases, so the reader should compare their environment with the assumptions in the post before copying commands or decisions.
Maintenance Standard
Recheck this guidance after dependency, operating-system, or build-tool changes. A useful update does not need to rewrite the entire post; it should confirm whether the examples, links, commands, screenshots, and decision criteria still match current behavior. If the old conclusion remains valid, record the check date. If it changes, explain what changed and why the previous advice is no longer enough.
Practical Questions Before Acting
Related Reading
Continue with these related posts from the same topic area.
Leave a comment