Tuesday, February 1, 2022

ItsMyCode: 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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...