Skip to content
Related Articles

Related Articles

Convert integer to string in Python

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 08 Jul, 2022
View Discussion
Improve Article
Save Article

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .format function or using f-string function.

Below is the list of possible ways to convert an integer to string in python:

1. Using str() function 

Syntax: str(integer_value)

Example:  

Python3




num = 10
  
# check  and print type of num variable
print("Type of variable before convertion : ", type(num)) 
  
# convert the num into string
converted_num = str(num)
  
# check  and print type converted_num variable
print("Type After convertion : ",type(converted_num))

Output:

Type of variable before convertion :  <class 'int'>
Type After convertion :  <class 'str'>

2. Using “%s” keyword

Syntax: “%s” % integer

Example: 

Python3




num = 10
  
# check  and print type of num variable
print("Type of variable before convertion : ", type(num)) 
  
# convert the num into string and print
converted_num = "% s" % num
print("Type after convertion : ", type(converted_num))

Output:

Type of variable before convertion :  <class 'int'>
Type after convertion :  <class 'str'>

3. Using .format() function

Syntax: ‘{}’.format(integer)

Example: 

Python3




num = 10
  
# check  and print type of num variable
print("Type before convertion : ", type(num)) 
  
# convert the num into string and print
converted_num = "{}".format(num)
print("Type after convertion :",type(converted_num))

Python-Foundation-Course

Output:

Type before convertion :  <class 'int'>
Type after convertion : <class 'str'>

4. Using f-string

Syntax: f'{integer}’

Example: 

Python3




num = 10
  
# check  and print type of num variable
print("Type before convertion : ",type(num)) 
  
# convert the num into string 
converted_num = f'{num}'
  
# print type of converted_num
print("Type after convetion : ", type(converted_num))

Output:

Type before convertion :  <class 'int'>
Type after convetion :  <class 'str'>

                                                                                                                    5. Using __str__() method 

Syntax:  Integer.__str__()

Python3




num = 10
  
# check  and print type of num variable
print("Type before convertion : ",type(num)) 
  
# convert the num into string 
converted_num = num.__str__()
  
# print type of converted_num
print("Type after convetion : ", type(converted_num))

Output:

Type before convertion :  <class 'int'>
Type after convetion :  <class 'str'>

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!