Python Program to Convert Integer to String

Write a Python program to convert integer to string there are many ways to achieve it, and this post covers all of them. For example, we can use the built-in str() function to convert int to string. Here, the type() function returns the data type.

num = 120

print(num)
print(type(num))

intToString = str(num)

print(intToString)
print(type(intToString))
Python Program to Convert Integer to String

Python Program to Convert Integer to String using format

The following list of examples uses all kinds of available option to convert the user-given integer to a string.

Using % formatter

In this example, we use the %s format, and it helps to convert int to string.

num = int(input("Enter Any Number = "))

print(num)
print(type(num))

intToString = "%s" %num

print(intToString)
print(type(intToString))
Enter Any Number = 12313
12313
<class 'int'>
12313
<class 'str'>

Using {} format function

This Python program helps to convert integers to strings using the format function.

num = int(input("Enter Any Number = "))

print(num)
print(type(num))

intToString = "{}".format(num)

print(intToString)
print(type(intToString))
Enter Any Number = 2345
2345
<class 'int'>
2345
<class 'str'>

Convert Integer to String using f keyword

This Python example uses the f keyword before the {} to convert int to string.

num = int(input("Enter Any Number = "))

print(num)
print(type(num))

intToString = f'{num}'

print(intToString)
print(type(intToString))
Enter Any Number = 4567
4567
<class 'int'>
4567
<class 'str'>

In this Python example, we combined or showed all the possible options to convert int to string.

num = int(input("Enter Any Number = "))

print(num)
print(type(num))

print("\nResult of str function")
intToString = str(num)
print(intToString)
print(type(intToString))

print("\nResult of %s Formatter")
intToString1 = "%s" %num
print(intToString1)
print(type(intToString1))

print("\nResult of format function")
intToString2 = "{}".format(num)
print(intToString2)
print(type(intToString2))

print("\nResult of f string")
intToString3 = f'{num}'
print(intToString3)
print(type(intToString3))
Python Program to Convert Integer to String using format, f, str() function