Open In App

Convert integer to string in Python

Last Updated : 07 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 conversion : ", type(num))
 
# convert the num into string
converted_num = str(num)
 
# check  and print type converted_num variable
print("Type After conversion : ",type(converted_num))


Output:

Type of variable before conversion :  <class 'int'>
Type After conversion :  <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 conversion : ", type(num))
 
# convert the num into string and print
converted_num = "% s" % num
print("Type after conversion : ", type(converted_num))


Output:

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

3. Using .format() function

Syntax: ‘{}’.format(integer)

Example: 

Python3




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


Output:

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

4. Using f-string

Syntax: f'{integer}’

Example: 

Python3




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


Output:

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

                                                                                                                    5. Using __str__() method 

Syntax:  Integer.__str__()

Python3




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


Output:

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

                                                                                                6. Using str.isdigit() 

Python3




str_value = "1234"
if str_value.isdigit():
    int_value = int(str_value)
    print(int_value)
    print(type(int_value))
else:
    raise ValueError("Invalid literal for int(): {}".format(str_value))


Output

1234
<class 'int'>

7.Using join() method:

 join() method is used to convert a list of integers to a string. We convert the integer to a list of characters using the list() function and then join them using the join() method.

Python3




num = 10
 
# check  and print type of num variable
print("Type before conversion : ",type(num))
 
# convert the num into string
converted_num = ''.join(list(str(num)))
 
# print type of converted_num
print("Type after conversion : ", type(converted_num))


Output

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

Time Complexity: O(N) where n is the number of digits in the integer. 

Space Complexity:O(N) as we need to create a list of characters that has n elements,



Similar Reads

Python | Convert list of string into sorted list of integer
Given a list of string, write a Python program to convert it into sorted list of integer. Examples: Input: ['21', '1', '131', '12', '15'] Output: [1, 12, 15, 21, 131] Input: ['11', '1', '58', '15', '0'] Output: [0, 1, 11, 15, 58] Let's discuss different methods we can achieve this task. Method #1: Using map and sorted() C/C++ Code # Python code to
4 min read
Python - Convert Alternate String Character to Integer
Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the alternate list of string to integers is quite common in development domain. Let’s discuss few ways to solve this particular problem. Method #1 : Naive Method This is most generic method that strikes any programmer while performing t
5 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
How to convert string to integer in Python?
In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() function and it will convert your string into an equivalent decimal integer. Syntax : int(string, base)
3 min read
Python - Convert Integer Matrix to String Matrix
Given a matrix with integer values, convert each element to String. Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]] Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] Explanation : All elements of Matrix converted to Strings. Input : test_list = [[4, 5, 7], [10, 8, 3]] Output : [['4', '5', '7'], ['10', '8', '3']] Explanation : A
6 min read
Convert Hex String To Integer in Python
Hexadecimal representation is commonly used in computer science and programming, especially when dealing with low-level operations or data encoding. In Python, converting a hex string to an integer is a frequent operation, and developers have multiple approaches at their disposal to achieve this task. In this article, we will explore various method
2 min read
Python Program to convert List of Integer to List of String
Given a List of Integers. The task is to convert them to a List of Strings. Examples: Input: [1, 12, 15, 21, 131]Output: ['1', '12', '15', '21', '131']Input: [0, 1, 11, 15, 58]Output: ['0', '1', '11', '15', '58']Method 1: Using map() [GFGTABS] Python3 # Python code to convert list of # string into sorted list of integer # List initialization list_i
5 min read
How to Convert String to Integer in Pandas DataFrame?
Let's see methods to convert string to an integer in Pandas DataFrame: Method 1: Use of Series.astype() method. Syntax: Series.astype(dtype, copy=True, errors=’raise’) Parameters: This method will take following parameters: dtype: Data type to convert the series into. (for example str, float, int).copy: Makes a copy of dataframe/series.errors: Erro
3 min read
Python Program to Convert a list of multiple integers into a single integer
Given a list of integers, write a Python program to convert the given list into a single integer. Examples: Input : [1, 2, 3] Output : 123 Input : [55, 32, 890] Output : 5532890 There are multiple approaches possible to convert the given list into a single integer. Let's see each one by one. Approach #1 : Naive Method Simply iterate each element in
4 min read
Python | Ways to convert Boolean values to integer
Given a boolean value(s), write a Python program to convert them into an integer value or list respectively. Given below are a few methods to solve the above task. Convert Boolean values to integers using int() Converting bool to an integer using Python typecasting. C/C++ Code # Initialising Values bool_val = True # Printing initial Values print(
3 min read