You’ve just started writing a Python program and then it hits you: a TypeError. This one’s new to you: “typeerror: can only concatenate str (not “int”) to str”. What does it mean? Why is it being raised in your code? These are all good questions.
In this guide, we’re going to talk about what this Python error means and how you can solve it. We’ll walk through an example program with this error so that you can see how it works. Without further ado, let’s get started!
The Problem: typeerror: can only concatenate str (not “int”) to str
In Python, values can only be concatenated if they are the same type. You cannot concatenate a string and an integer, or a string and a list. If you do, a TypeError is raised:
typeerror: can only concatenate str (not "int") to str
There are a few programming languages, like JavaScript, which allow you to concatenate strings and integers together. In Python, you cannot do this. If you want to concatenate a string and an integer, you’ve got to convert the integer to a string first before you concatenate it to a string.
An Example Situation
This error is raised when you try to concatenate a string and an integer.
Let’s take a look at a program which suffers from this problem:
gatsby = { "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "price": 4.99, "quantity_in_stock": 4 } print("There are " + gatsby["quantity_in_stock"] + " copies of The Great Gatsby in stock.")
This program prints out how many copies of the book The Great Gatsby are in stock at a bookstore. First, we declare a dictionary called “gatsby”. This dictionary contains four pieces of information about the book: its title, the author, its price, and how many copies are in stock.
Next, we print out a message informing the user how many copies are in stock at the bookstore.
Let’s try to run our code:
Traceback (most recent call last): File "main.py", line 8, in <module> print("There are " + gatsby["quantity_in_stock"] + " copies of The Great Gatsby in stock.") TypeError: can only concatenate str (not "int") to str
As we expected, a TypeError has been raised.
The Solution
The value of gatsby[“quantity_in_stock”] is an integer. A TypeError has been raised because we have tried to concatenate that value, an integer, to a string.
We can solve this problem by converting the value of gatsby[“quantity_in_stock”] to a string before we concatenate it to our other strings. We can do this using the str() method, which converts an integer to a string:
print("There are " + str(gatsby["quantity_in_stock"]) + " copies of The Great Gatsby in stock.")
Let’s try to run our code again:
There are 4 copies of The Great Gatsby in stock.
Our code has found how many copies of The Great Gatsby are in stock. Then, it has printed out how many copies are in stock to the console. This value is printed in the following format:
There are X copies of The Great Gatsby in stock.
“X” refers to how many copies are in stock.
Conclusion
The error “typeerror: can only concatenate str (not “int”) to str” is raised when you try to concatenate a string and an integer. To solve this error, make sure that all values in a line of code are strings before you try to concatenate them.
Now you’re ready to solve this Python TypeError like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.