3 Ways to Concatenate String and Int in Python — CODEFATHER

Claudio Sabato
5 min readNov 5, 2023

Are you here because you have tried to concatenate a string and an int in Python and you have seen an error? You are in the right place to fix it.

In Python, you cannot concatenate a string and an int using the concatenation operator (+). One way to make the concatenation work is to convert the int into a string first using the str() function. Other options are to use the string format() method or f-strings.

Let’s see some examples of how to concatenate a string to an int!

1. Concatenate a String to an Int using the str() Function

Open the Python shell. In this example, we will create one string and one int variable and then we will try to concatenate them using the concatenation operator (+).

This is quite a common thing you would do in a Python program, for example, if you want to show a user the number of points they have reached in a game you are programming.

>>> message = "Your points are: "
>>> number_of_points = 23
>>> print(message + number_of_points)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

In this example, we want to print the message:”Your points are: 23".

But as you can see, Python raises the TypeError "can only concatenate str (not "int") to str" because it cannot concatenate an integer to a string. It can only concatenate a string to another string.

Explanation: a TypeError occurs in Python when an operation is applied to a variable (object) of incorrect type.

How can you fix this error?

One way to concatenate a string to an integer is to first convert the integer into a string using the str() function that returns the string version of the integer.

>>> print(message + str(number_of_points))
You points are: 23

This time the print() function doesn’t raise any errors and you can see the full message printed.

This is not the only way to solve this error, let’s see what other options you have so you can choose the one you prefer.



Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.