Fixing TypeError: can only concatenate str (not "int") to str

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
5 min. read Python 3.7—3.11
Share
Copied to clipboard.
Tags

Your Python program crashed and displayed a traceback like this one:

Traceback (most recent call last):
  File "copyright.py", line 4, in <module>
    statement = "Copyright " + year
TypeError: can only concatenate str (not "int") to str

Or maybe you saw a variation on that message:

Traceback (most recent call last):
  File "names.py", line 3, in <module>
    print("Hello" + names)
TypeError: can only concatenate str (not "list") to str

Or you may have seen a different error message that mentions + and int and str:

Traceback (most recent call last):
  File "add.py", line 5, in <module>
    print(start + amount)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

What do these mean? And how can you fix your code?

What does that error message mean?

The last line is in a Python traceback message is the most important one.

TypeError: can only concatenate str (not "int") to str

That last line says the exception type (TypeError) and the error message (can only concatenate str (not "int") to str).

This error message says something similar:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Both of these error messages are trying to tell us that we're trying to use the + operator (used for string concatenation in Python) between a string and a number.

You can use + between a string and a string (to concatenate them) or a number and a number (to add them). But in Python, you cannot use + between a string and a number because Python doesn't do automatic type coercion. Type conversions are usually done manually in Python.

How do we fix this?

How we fix this will depend on our needs.

Look at the code the error occurred on (remember that tracebacks are read from the bottom-up in Python).

  File "copyright.py", line 4, in <module>
    statement = "Copyright " + year

Ask yourself "what am I trying to do here?" You're likely either trying to:

  1. Build a bigger string (via concatenation)
  2. Add two numbers together
  3. Use + in another way (e.g. to concatenate lists)

Once you've figured out what you're trying to do, then you'll then need to figure out how to fix your code to accomplish your goal.

Converting numbers to strings to concatenate them

If the problematic code is meant to concatenate two strings, we'll need to explicitly convert our non-string object to a string. For numbers and many other objects, that's as simple as using the built-in str function.

We could replace this:

statement = "Copyright " + year

With this:

statement = "Copyright " + str(year)

Though in many cases it may be more readable to use string interpolation (via f-strings):

statement = f"Copyright {year}"

This works because Python automatically calls the str function on all objects within the replacement fields in an f-string.

Converting other objects to strings

Using the str function (or an f-string) will work for converting a number to a string, but it won't always produce nice output for other objects.

For example when trying to concatenate a string and a list:

>>> names = ["Judith", "Andrea", "Verena"]
>>> user_message = "Users include: " + names
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str

You might think to use str to convert the list to a string:

>>> user_message = "Users include: " + str(names)

This does work, but the resulting string might not look quite the way you'd expect:

>>> print(user_message)
Users include: ['Judith', 'Andrea', 'Verena']

When converting a list to a string you'll likely want to join together the list items using the string join method.

>>> user_message = "Users include: " + ", ".join(names)
>>> print(user_message)
Users include: Judith, Andrea, Verena

Other types of objects may require a different approach: the string conversion technique you use will depend on the object you're working with and how you'd like it to look within your string.

Converting strings to numbers to add them

What if our code isn't supposed to concatenate strings? What if it's meant to add numbers together?

If we're seeing one of these two error messages while trying to add numbers together:

  • TypeError: can only concatenate (not "int") to str
  • TypeError: unsupported operand type(s) for +: 'int' and 'str'

That means one of our "numbers" is currently a string!

This often happens when accepting a command-line argument that should represent a number:

$ python saving.py 100
Traceback (most recent call last):
  File "saving.py", line 7, in <module>
    total += sys.argv[1]
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

Command-line arguments are often an issue because all command-line arguments come into Python as strings. So the sys.argv list in that saving.py program might look like this:

>>> sys.argv
['saving.py', '100']

Note: If command-line arguments are the issue, you may want to look into using argparse to parse your command-line arguments into the correct types.

This numbers-represented-as-strings issue can also occur when prompting the user with the built-in input function.

>>> total = 0
>>> user_input = input("Also add: ")
Also add: 100
>>> total += user_input
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

The input function stores everything the user entered into a string:

>>> user_input
'100'

Regardless of how it happened, if you want to convert your string which represents a number to an actual number, you can use either the built-in float function or the built-in int function:

>>> user_input
'100'
>>> deposit = float(user_input)
>>> deposit
100.0
>>> deposit = int(user_input)
>>> deposit
100

The float function will accept numbers with a decimal point. The int function will only accept integers (numbers without a decimal point).

Remember: type conversions usually need to be explicit

That can only concatenate str (not "int") to str error message shows up frequently in Python because Python does not have type coercion.

Whenever you have a string and a non-string and you'd like to either sum them with + or concatenate them with +, you'll need to explicitly convert your object to a different type.

All data that comes from outside of your Python process starts as binary data (bytes) and Python typically converts that data to strings. Whether you're reading command-line arguments, reading from a file, or prompting a user for input, you'll likely end up with strings.

If your data represents something deeper than a string, you'll need to convert those strings to numbers (or whatever type you need):

>>> c = a + int(b)
>>> a, b, c
(3, '4', 7)

Likewise, if you have non-strings (whether numbers, lists, or pretty much any other object) and you'd like to concatenate those objects with strings, you'll need to convert those non-strings to strings:

>>> year = 3000
>>> message = "Welcome to the year " + str(year) + "!"
>>> print(message)
Welcome to the year 3000!

Though remember that f-strings might be a better option than concatenation:

>>> year = 3000
>>> message = f"Welcome to the year {year}!"
>>> print(message)
Welcome to the year 3000!

And remember that friendly string conversions sometimes require a bit more than a str call (e.g. converting lists to strings).

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.