TypeError: can only concatenate str (not “int”) to str

In Python, we can concatenate values if they are of the same type. Let’s say if you concatenate a string and an integer you will get TypeError: can only concatenate str (not “int”) to str

In this article, we will take a look at what TypeError: can only concatenate str (not “int”) to str means and how to resolve this error with examples.

What is TypeError: can only concatenate str (not “int”) to str

Unlike other programming languages like JavaScript, Python does not allow concatenating values of different types. For Example, we cannot concatenate a string and an integer, string and a list etc.

Example Scenario

Let us take a simple example to reproduce this issue.

product = {
	"item": "iPhone",
	"price": 1599,
	"qty_available": 40
}

print("We have total " + product["qty_available"] + " quantities of Product " + product["item"])

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 7, in <module>
    print("We have total" + product["qty_available"] + "quantities of " + product["item"])
TypeError: can only concatenate str (not "int") to str

When we run our code we get TypeError: can only concatenate str (not “int”) to str because we are trying to concatenate the string and integer value in our print statement.

How to fix TypeError: can only concatenate str (not “int”) to str

In Python, you should ensure that the values are of the same type before performing the concatenation. Usually, we come under into this situation often during computational or printing the dictionary value using the print statement.

In the above dictionary, the product[qty_available] is of type integer and we are concatenating the integer value with strings in the print statement which leads into TypeError: can only concatenate str (not “int”) to str.

Solution

Now that we know why the issue occurs in the first place the solution is pretty simple and straightforward.

We can resolve the issue by converting the value product[qty_available] to a string before concatenating with other strings in the print statement.

Let us modify the above example and run it once again.

product = {
	"item": "iPhone",
	"price": 1599,
	"qty_available": 40
}

print("We have total " + str(product["qty_available"]) + " quantities of Product " + product["item"])

Output

We have total 40 quantities of Product iPhone

Conclusion

The TypeError: can only concatenate str (not “int”) to str mainly occurs if you try to concatenate integer with a string. Python does not allow concatenating values of different types. We can resolve the issue by converting the integer values to strings before concatenating them in the print statement.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Callable()

Python callable()

Table of Contents Hide callable() Syntax callable() Parameterscallable() Return ValueExample 1: How does callable() works?Example 2: When an Object is callable Example 3: When an Object is NOT callable  The callable() function in…
View Post
Python Remove Newline From String

Python Remove Newline From String

Table of Contents Hide Python Remove Newline From StringUsing strip() method to remove the newline character from a stringUsing replace() method to remove newlines from a stringUsing regex to remove…
View Post
Python Slice()

Python slice()

Table of Contents Hide slice() Syntax slice() Parametersslice() Return ValueExample 1: Python slice string Get substring using slice objectExample 2: Get substring using negative indexExample 3: Python slice list or Python slice arrayExample 4: Python…
View Post
Python String Endswith()

Python String endswith()

Table of Contents Hide endswith() Syntaxendswith() Parameterendswith() Return ValueExample 1: endswith() Without start and end ParametersExample 2: endswith() With start and end ParametersPassing Tuple to endswith() Python String endswith() method…
View Post
How To Fix Keyerror In Pandas

How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…
View Post