Skip to content

Convert int to string Python | Example code

  • by

Use the built-in str() function to convert int to string in Python. The str() function takes in any data type and converts it into a string object.

str(integer_value)

Example how to convert int to string Python

A simple example code converts int to str in Python. Just pass the int value into str() function.

n = 100

res = str(n)

print(type(res), res)

Output:

Convert int to string Python

Alternative method

Use the “%s” keyword, the format function, or using f-string function.

num = 100

# % Operator
print(type("% s" % num))

# format() function
print(type("{}".format(num)))

# f-strings
print(type(f'{num}'))

If you want to concatenate an integer with a string, you can also use the str() function to convert the integer to a string and then use the + operator to concatenate the two strings.

age = 25
message = "I am " + str(age) + " years old."
print(message)  # Output: "I am 25 years old."

Comment if you have any doubts or suggestions on this Python integer-to-string code.

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