Last modified: Jan 10, 2023 By Alexander Williams

How To Solve TypeError: can only concatenate str (not 'int') to str in Python


In this article, we'll talk about solutions of the "TypeError: can only concatenate str (not "int") to str issue appears" issue.

Before starting, you need to know that issue appears when you try to concatenate a string with an integer.

For example:


num = 99

concatenate = "python" + num

print(concatenate)

Output:

Traceback (most recent call last):
File "test.py", line 5, in <module>
    concatenate = "python" + num
TypeError: can only concatenate str (not "int") to str

To solve this issue, we'll use five methods.

Using str() method

The str() method converts the given value to a string.

How to use it:


str(num)

Example:


num = 99

concatenate = "python" + str(num)

print(concatenate)

Output:

python99

Using fstring function

f-string() inserts a variable into a string, and we'll use it to solve the issue.

Note: this method works on Python >= 3.6.

Example:


num = 99

concatenate = f"python{num}"

print(concatenate)

Output:

python99

Using .format() function

theformat() function doing the same thing as f-string().

Example:


num = 99

concatenate = "python{}".format(num)

print(concatenate)

Output:

python99

Using the "%" symbol

You can also use the"%" symbol methods to concatenate a string with an integer.

Example:


num = 99

concatenate = "python%s"%num

print(concatenate)

Output:

python99