Convert Integer to String in Python

  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

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.

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.

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'
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Integer

  • Concatenate String and Int Values in Python
  • Check if a String Is Integer in Python
  • Related Article - Python String

  • Remove Quotes From String in Python
  • String Capitalization in Python