Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert int to string in Python?
Type conversion is often needed when you want to convert one data type into another according to your requirements. Python provides several methods to convert integers to strings.
Python has a built-in function str() to convert an integer to a string. We will discuss various methods to convert int to string in Python.
Using str() Function
This is the most commonly used method to convert int to string in Python. The str() function takes an integer variable as a parameter and converts it into a string ?
Syntax
str(integer_variable)
Example
num = 2
print("Datatype before conversion:", type(num))
num = str(num)
print("Converted value:", num)
print("Datatype after conversion:", type(num))
Datatype before conversion: <class 'int'> Converted value: 2 Datatype after conversion: <class 'str'>
The type() function returns the datatype of the variable passed as a parameter. Before conversion, the datatype is int, and after conversion, it becomes str.
Using f-string (Formatted String Literals)
F-strings provide a modern and readable way to convert integers to strings ?
Syntax
f'{integer_variable}'
Example
num = 2
print("Datatype before conversion:", type(num))
num = f'{num}'
print("Converted value:", num)
print("Datatype after conversion:", type(num))
Datatype before conversion: <class 'int'> Converted value: 2 Datatype after conversion: <class 'str'>
Using "%s" Formatting
This method uses old-style string formatting with the % operator ?
Syntax
"%s" % integer_variable
Example
num = 2
print("Datatype before conversion:", type(num))
num = "%s" % num
print("Converted value:", num)
print("Datatype after conversion:", type(num))
Datatype before conversion: <class 'int'> Converted value: 2 Datatype after conversion: <class 'str'>
Using .format() Method
The format() method provides another way to convert integers to strings ?
Syntax
'{}'.format(integer_variable)
Example
num = 2
print("Datatype before conversion:", type(num))
num = '{}'.format(num)
print("Converted value:", num)
print("Datatype after conversion:", type(num))
Datatype before conversion: <class 'int'> Converted value: 2 Datatype after conversion: <class 'str'>
Comparison of Methods
| Method | Syntax | Best For |
|---|---|---|
str() |
str(num) |
Simple conversion, most readable |
| f-string | f'{num}' |
Modern Python, part of larger strings |
| % formatting | "%s" % num |
Legacy code compatibility |
| .format() | '{}'.format(num) |
Complex formatting requirements |
Common Use Cases
Converting integers to strings is useful in scenarios such as:
- Concatenating numbers with text strings
- Reversing an integer (convert to string, reverse, then convert back)
- Creating formatted output for display purposes
- Writing data to files that require string format
Conclusion
The str() function is the most straightforward method for converting integers to strings. Use f-strings for modern Python applications and when embedding integers within larger strings.
