How to Convert Integer to String in Python

Minahil Noor Feb 02, 2024
  1. Use the str() Function to Convert an Integer to a String in Python
  2. Use the f-Formatting to Convert an Integer to a String in Python
How to Convert Integer to String in Python

This article will introduce different methods to convert an integer to a string using Python code, like the str() function and the f-Formatting method.

Use the str() Function to Convert an Integer to a String in Python

We can use the built-in function str() in Python to convert an integer to a string. The correct syntax to use this method is as follows:

str(objectName)

This function accepts one parameter only. The detail of its parameter is as follows:

Parameters Description
objectName mandatory It is the object we want to convert to a string. In our case, it will be the integer.

If we do not convert an integer to a string and try to use it with the string, then the function gives an error.

# python 2.x
integer = 4
print "My number is " + integer

Output:

TypeError: cannot concatenate 'str' and 'int' objects

The program below shows how we can use this method to convert an integer to a string in Python.

# python 2.x
integer = 4
mystring = str(integer)
print "My number is " + mystring

Output:

My number is 4

Use the f-Formatting to Convert an Integer to a String in Python

In Python, we can also use the f-formatting to convert an integer to a string. It is an efficient way of formatting an integer. The correct syntax to use this is as follows:

f"{intName}"

It accepts one parameter only. The detail of its parameter is as follows:

Parameters Description
intName mandatory It is the integer that we want to convert to a string.

The program below shows how we can use f-formatting to convert an integer to a string in Python.

integer = 8
f"{integer}"

Output:

8

Related Article - Python Integer

Related Article - Python String