Python is an object-oriented programming language and everything that exists in a Python environment is of type object. Even texts and numbers are also an object in Python. There are several data types in Python using which we represent our data line int, float, string, Boolean, etc. The data types are a type of class to which these objects belong. Python is an interpreter dependent language and in which data type of an object is interpreted at the runtime automatically by the interpreter. It does not require data types of a variable to be declared before compilation.

While programming, there are certain cases where we require our integers to be represented as string. So, in this article we are going to read about four different techniques using which we can convert integers to strings in Python.

Method 1: Using built-in str() function

When an integer is passed as an argument inside str() method, it directly converts int data type to str data type.

Example 1: Program to convert integers to string using built-in str() method.

A = 1 # declaring a variable A which stores an integer having value 1
print(type(A)) # verifying the data type of variable A is int
A = str(A) # passing the variable A as an argument in str() method and reassigning the value back to A
print(type(A)) # Checking if datatype of A has changed or not.

Output:

 Hence, it is verified that int data type is converted into str data type.

Method 2: Using .format() function

It conerts integers to string and prints them directly.

Example 2: Program to convert integers to string using .format() function.

A = 1 # declaring a variable A which stores an integer having value 1
print(type(A)) # verifying the data type of variable A is int
A = "{}".format(A) # passing the variable A as an argument in .format() method and reassigning the value back to A
print(type(A)) 

Output:

Method 3: Using “%s” keyword

We can even use the %s keyword to convert integers into strings.

Example 3: Program to convert integers to string using “%s” keyword.

A = 1
print(type(A)) 
print("% s" %A) # converting the integer into string and printing its value. 

Output:

Method 4: Using f-string method

A = 1 # declaring a variable A which stores an integer having value 1
print(type(A))  # verifying the data type of variable A is int
A = f'{A}' # passing the variable A as an argument in f-string method and reassigning the value back to A
print(type(A)) # checking if datatype of A has changed or not

Output: