Python Program to Convert Integer to String

Write a Python program to convert integer to string. There are many ways to achieve convert integer to string, and they are using the built-in str()function, % formatter, {} format function, and f keyword within the print statement. This post covers all of them.

Convert Integer to String using str() function

In this programming language, there is a built-in str() function to convert int to string. The below example uses the built-in str() function to perform the conversion. Here, the type() function returns the data type.

num = 120

print(num)
print(type(num))

intToString = str(num)

print(intToString)
print(type(intToString))
120
<class 'int'>
120
<class 'int'>

Python Program to Convert Integer to String using format

The following list of examples uses all kinds of available options 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 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 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 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