Skip to content

Python concatenate string and int | Example code

  • by

Use the str() function to concatenate string and int in Python. If you try to concatenate string and int using + operator, the program will throw a runtime error.

However, since you can’t concatenate two objects of different types, you need to convert the integer to a string before concatenating it with the string.

Example concatenate string and int in Python

Simple example code using str() function. You have to pass the int value into the str() function to convert it.

After converting int into a string you can use the + operator.

s = "Hello"
n = 100

print("Hello" + str(n))

Output:

Python concatenate string and int

Other methods

Using % Operator, format() function or f-strings can use too for concatenation of string and int.

s = "Hello"
n = 100

# % Operator
print("%s%s" % (s, n))

# format() function
print("{}{}".format(s, n))

# f-strings
print(f'{s}{n}')

Output:

Hello100
Hello100
Hello100

Comment if you have any doubts or suggestions on this Python concatenate topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading