Convert integer to string in Python
Last Updated :
25 Apr, 2025
In this article, we’ll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.
Using str() Function
str() function is the simplest and most commonly used method to convert an integer to a string.
Python
n = 42
s = str(n)
print(s)
Explanation: str(n) converts n to a string, resulting in '42'.
Using f-strings
For Python 3.6 or later, f-strings provide a quick way to format and convert values.
Python
n = 42
s = f"{n}"
print(s)
Explanation: The {n} inside the f-string automatically converts n to a string.
format() function inserts values into {} placeholders in a string. This is similar to f-strings but works with older versions of Python (before 3.6).
Python
n = 42
s = "{}".format(n)
print(s)
Explanation: format() places n into {}, converting it to '42'
Using %s Keyword
The %s keyword allows us to insert an integer (or any other data type) into a string. This method is part of the older style of string formatting but still works in Python.
Python
n = 42
s = "%s" % n
print(s)
Explanation: The %s keyword acts as a placeholder within the string and automatically converts n to a string before inserting it in place of %s. This approach can be useful for quick formatting but is less commonly used than f-strings or format().
Using repr() for Debugging
repr() function is usually used for debugging, as it gives a detailed string version of an object. While it also converts integers to strings, it’s not the most common method for simple integer-to-string conversion.
Python
n = 42
s = repr(n)
print(s)
Explanation: repr(n) returns a string '42'. It’s similar to str() but usually used for debugging.
Related Articles:
Similar Reads
Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver
3 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 equiv
3 min read
Convert String to Long in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting a string to long. Converting String to long A long is an integer type value that has unlimited length. By converting a string into long we are transl
1 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Convert Hex to String in Python Hexadecimal (base-16) is a compact way of representing binary data using digits 0-9 and letters A-F. It's commonly used in encoding, networking, cryptography and low-level programming. In Python, converting hex to string is straightforward and useful for processing encoded data.Using List Comprehens
2 min read