Learn Python – How to convert int to string in Python- Basic and advance

We can convert an integer information kind using the Python built-in str() function. This function takes any records type as an argument and converts it into a string. But we can additionally do it using the “%s” literal and the usage of the .format() function. Below is the syntax of the str() function.

Syntax –

str(integer_Value)  

Let’s understand the following example.

Example – 1 Using the str() function

n = 25  
# check  and print type of num variable  
print(type(n))  
print(n)  
  
# convert the num into string  
con_num = str(n)  
  
# check  and print type converted_num variable  
print(type(con_num))  
print(con_num)  

Output:

<class 'int'>
25
<class 'str'>
25

Example – 2 Using the “%s” integer

n = 10  
  
# check and print type of n variable  
print(type(n))  
  
# convert the num into a string and print  
con_n = "% s" % n  
print(type(con_n))  

Output:

<class 'int'>
<class 'str'>

Example – 3: Using the .format() function

n = 10  
  
# check  and print type of num variable  
print(type(n))  
  
# convert the num into string and print  
con_n = "{}".format(n)  
print(type(con_n))  

Output:

<class 'int'>
<class 'str'>

Example – 4: Using f-string

n = 10  
  
# check  and print type of num variable  
print(type(n))  
  
# convert the num into string  
conv_n = f'{n}'  
  
# print type of converted_num  
print(type(conv_n))   

Output:

<class 'int'>
<class 'str'>

We have described all methods of changing the integer data kind to the string type. You can use one of them in accordance to your requirement.