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

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Jsonpath

Python JSONPath

Table of Contents Hide JSONPath Library in PythonInstalling jsonpath-ng ModuleJsonpath operators:Parsing a Simple JSON Data using JSONPathParsing a Json Array using JSONPath Expression JSONPath is an expression language that is…
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 Title()

Python String title()

Table of Contents Hide title() Syntaxtitle() Parameterstitle() Return ValueExample 1: How Python title() works?Example 2: title() method in case of number and apostrophes The Python String title() method is a…
View Post