How to Fix the Python Error: TypeError: can only concatenate str (not “int”) to str

One of the best things about Python is the sheer power and freedom offered by its type system. The language gives programmers far more freedom to easily manipulate variables than they would in, for example, C++. But at the same time variables are controlled enough to avoid some of the pitfalls of languages like Javascript. The end result is a language where you can easily work with types while still gaining some built-in protection from conflicts.

However, that’s not to say that the Python interpreter will smoothly handle everything related to variable type. Type errors, technically referred to as a TypeError, are almost an occasional inevitability of using Python. But if you’re seeing a “TypeError: can only concatenate str (not “int”) to str” then rest assured that you’ll soon know exactly how to fix it.

The TypeError’s Central Problem

The central issue seen in the “TypeError: can only concatenate str (not “int”) to str” comes down to incompatible concatenation. Concatenation is a process by which you can join multiple strings together. But it’s important to remember that concatenation requires compatible data types. Python makes it easy to convert values between different types. But the interpreter usually won’t perform the action automatically. If you try to concatenate integers and strings together you’ll get a “TypeError: can only concatenate str (not “int”) to str” error.

A Deeper Dive Into Python’s Types

This Python error most commonly occurs when programmers think that they’re working with one data type but are actually using two. For example, you might be using both integers and strings together while thinking that you’re only working with strings. The situation might not seem very likely at first. After all, an integer and string typically contain very different data. But keep in mind that strings can contain numbers. They’re just numbers in string format.

For example, imagine that you’re grabbing numerical data from both an internal calculation and a text file. Or that you’re trying to concatenate list data from a similar source. You want to lay out the result as a three-digit string. We’ll also assume that you already have the data for the tens position and want to concatenate it between digits from a text file. We can simplify and illustrate this as seen in the following Python code.

x = “4”
y = 5
z = “6”
print(x + y + z)

This example produces the TypeError error message because “4” and “6” are strings while 5 is an integer. In other words, only the y variable contains a true number while the other variables are strings that contain characters representing numbers. Note that the order of operations is significant here.

Print will convert data to a string. But keep in mind that Python’s interpreter will handle all of the calculations inside the print statement first. So we start out with “4” and use + as the operator that’s concatenating values together. But the problem comes up when the concatenation hits the int value in y. If we were only passing y to print it’d be automatically converted to a string. But since we’re using string concatenation before the print function has a chance to run we never get to that stage.

In some languages working with both an integer value and string would require a lot of extra work. But this Python TypeError is fairly easy to fix thanks to the language’s built-in functions.

How To Fix the TypeError

When you try to solve TypeError issues you’ll typically be able to take advantage of the fact that Python objects often have properties that make conversations fairly simple. Most TypeErrors revolve around a common problem. We need to compensate for the different types used together. This is most easily done by converting the problematic value to a compatible form. In this case, we have an integer that we want in string format. And this is easily accomplished thanks to the fact that Python has a function to convert most data types into a string. Consider the following example.

x = “4”
y = 5
z = “6”
print(x + str(y) + z)

Everything is fairly similar to the original code sample. The only difference is that we convert the y variable into a string. At this point all of the variables are strings. Functions and operators that format strings will now work on all of the variables. This, of course, means that we can concatenate all of the variables together. Now when we run this code it will print out 456 without displaying any error messages. But we can also flip this concept around and work with an integer.

x = “4”
y = 5
z = “6”
print(int(x + str(y) + z))

This example is fairly similar to the previous approach to the TypeError. We still convert y to a string. However, the string concatenation result is then passed to int. This converts the “456” string to an int object type.

Note that we still convert y into a string despite the fact that we’re working toward a new integer. The order of operations performs this conversion first and then concatenates the strings. The strings are then passed to int. In this example, we don’t actually do anything special with the resulting integer. But it could be used for any calculations past that point rather than just passing it to print.

We still use concatenation rather than converting each variable to an integer first because the string concatenation preserves the order of the numbers. Where if we converted the variables to an integer first, we’d end up adding them together rather than concatenating them into a single three-digit number.

How to Fix the Python Error: TypeError: can only concatenate str (not “int”) to str
Scroll to top