Skip to content

Python concatenate strings and int

  • by

Using the str() Function you can concatenate strings and int in Python. Passing an int value to the str() function it will be converted to a str.

Python concatenates strings and int

Simple example code.

s = 'This Year is '

y = 2022

print(s + str(y))

Output:

Python concatenates strings and int

Using the % Interpolation Operator

print("%s%s" % (s, y))

Using the str.format() function

print("{}{}".format(s, y))

Using f-strings

print(f'{s}{y}')

Using a for-in loop with a range

string = 'string'
for i in range(11):
    string += str(i)
print(string)

Output: string012345678910

Comment if you have any doubts or suggestions on this Python concatenates 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