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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer. To solve the error, convert the int to a string, e.g. str(my_int) to concatenate the strings or convert the str to an int, e.g. int(my_str) to add the numbers.

typeerror can only concatenate str not int to str

Here is an example of how the error occurs.

main.py
# ⛔️ TypeError: can only concatenate str (not "int") to str result = 'hi' + 123
We tried to use the addition (+) operator to concatenate a string and an integer which caused the error.

The values on the left and right-hand sides need to be of compatible types.

One way to solve the error is to convert the integer to a string.

main.py
result = 'hi' + str(123) print(result) # 👉️ 'hi123'

We passed the integer to the str() class and converted it to a string before concatenating the two strings.

If you have a number that is wrapped in a string and an integer, you need to convert the string to an integer (or float) to add the two numbers.

main.py
result = int('50') + 50 print(result) # 👉️ 100

We passed the string to the int() class to convert it to an integer. Note that you can also use the float() class if you need to convert a string to a floating-point number.

IMPORTANT: if you use the input() built-in function, all of the values the user enters get converted to strings (even numeric values).

An alternative to concatenating strings with the addition (+) operator is to use a formatted string literal.

main.py
str_1 = 'abc' num_1 = 123 result = f'{str_1} {num_1} def' print(result) # 👉️ 'abc 123 def'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
str_1 = 'abc' print(type(str_1)) # <class 'str'> print(isinstance(str_1, str)) # 👉️ True num_1 = 123 print(type(num_1)) # <class 'int'> print(isinstance(num_1, int)) # 👉️ True

The type class returns the type of an object.

The error often occurs when getting user input using the built-in input() function.

main.py
num = input('Enter your fav number: ') print(num) # 👉️ '10' print(type(num)) # 👉️ <class 'str'> result = num + 100 # ⛔️ TypeError: can only concatenate str (not "int") to str print(result)

The input function converts the data to a string and returns it.

Even if the user enters a number, it gets converted to a string before it is returned from input().

If you need to convert the result to a number, pass it to the int() (or float()) class.

main.py
num = input('Enter your fav number: ') print(num) # 👉️ '10' print(type(num)) # 👉️ <class 'str'> # ✅ convert to int result = int(num) + 100 print(result) # 👉️ 110

We converted the string we got from the input function back to an integer and added the two numbers.

The values we are adding with the addition (+) operator must be of compatible types, e.g. a float and an int.

We can't simply add a string and an integer using the addition (+) operator as that causes the error.

Conclusion #

The Python "TypeError: can only concatenate str (not "int") to str" occurs when we try to concatenate a string and an integer. To solve the error, convert the int to a string, e.g. str(my_int) to concatenate the strings or convert the str to an int, e.g. int(my_str) to add the numbers.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.