While programming in python, many operations require typecasting of data and variables from one data type to another. These conversions are sometimes made by interpreters internally while running the program. But often interpreter also fails to perform the type conversion of data type, so the programmer has to perform explicit type conversion using some default methods. Today, let us understand the conversion of Int to String in python using 4 different methods in detail, along with the example and output. But before that, take a look at a brief introduction to integer and strings in python for better understanding.
What is Integer in Python?
Int or integer in python is a whole number, either positive or negative, but without decimal points. Integers have lengths up to the memory limit of a computer, and therefore, you may never run out of integers that you can use while programming in python. By default, int is decimal, i.e., base 10, but you can also define them or use them with different bases. All integer literals or variables are the objects of the “int” class. You can use the “type()” method to identify the class name of the variable in python.
For Example
a = 10 print(type(a)) print(a)
Output
<class 'int'> 10
What are Strings in Python?
In python, the string is a sequence of characters wrapped around single or double quotes. You can use triple quotes to create a multi-line string in python. It is immutable in nature, which means the strings, once defined, cannot be changed again. As python does not support character data type, a single character is considered a string of length 1. There are many string manipulation methods available to perform different operations on a string. For example, join, concatenation, append, trim, etc.
These methods are not directly applied to the original string because of their immutable nature. Therefore, a copy of the original string is created, and operations are performed on it. You can use square brackets to access the element of the string using their index number.
For Example
a = 'favtutor' print(type(a)) print(a)
Output
<class 'str'> favtutor
Convert Int to String in Python
There are total four methods by which you can convert the integer data type into string in python. Let us study all these methods in detail below:
1) Using str() function
str() is a built-in function in python programming used to convert the integer into a string. Apart from integer conversion, the str() function can take any python data type as a parameter and convert it into the string format. Take a look at the example below for a better understanding.
For Example
a = 10 print(type(a)) # converting int into string convert_a = str(a) print(type(convert_a))
Output
<class 'int'> <class 'str'>
2) Using string formatting
String formatting is one of the methods to insert a variable or another string into a predefined string. But apart from this, you can also use string formatting to convert an integer to a string. The “%s” keyword is used to convert int into a string. The empty string is created, and the “%s” operator is placed in it. Later, the integer which is to be converted into the string is defined. During the program execution, the interpreter will convert the int into a string as shown in the below given example:
For Example
a = 10 print(type(a)) # converting int into string convert_a = "% s" % a print(type(convert_a))
Output
<class 'int'> <class 'str'>
3) Using format() method
Just like the “%s” operator, you can also make use of the format() method to convert int into strings in python programming. For this method to work, you have to insert the {} placeholder in the empty string created. Later, you can call the format() method on the empty string with the integer to be converted. The below example shows the clear working of format() method for conversion of integer to string.
For Example
a = 10 print(type(a)) # converting int into string convert_a = "{}".format(a) print(type(convert_a))
Output
<class 'int'> <class 'str'>
4) Using F-strings
In python, F strings are used to embed a value or an expression into a string. But along with this, it is also used in the conversion of integers into strings. The syntax for F-strings is similar to that if format() method. The only difference between them is that you have to insert the variables directly into the placeholder while working with F-strings. This will make the code more clean and readable. Check out the below example for a better understanding.
For Example
a = 10 print(type(a)) # converting int into string convert_a = f'{a}' print(type(convert_a))
Output
<class 'int'> <class 'str'>
Conclusion
Great work! You’ve learned so much about the integer, strings, and different methods used to convert an integer (Int) to string in python. These methods include the use of the str() method, “%s” operator, format() method, and F-strings to convert the int to string explicitly. While programming in python, there are situations where it is necessary to perform the type conversions. Therefore, these methods will give effective and faster outputs for conversion of int to string data type.
To learn more about type conversion in python, refer to our article defining 6 different ways to convert string to float.