You are currently viewing Convert Integer to String in Python

The best method to convert an integer to a string is to use the Python str() function. However, there are other methods as well, which we will discuss in this article with examples. Type conversion is most used in any programming language.

Advertisements

1. Quick Examples of Converting Integer to String

These examples will give you a high-level idea of how to convert an integer to a string. Later on, we will discuss each of these methods in detail.


# Quick Examples of Converting Integer to String

# Method 1: The str() function
num = 465
str_num = str(num)

# Method 2: Recursion
def int_to_str(num):
    if num < 0:
        return '-' + int_to_str(-num)
    elif num < 10:
        return chr(ord('0') + num)
    else:
        return int_to_str(num // 10) + int_to_str(num % 10)

str_num = int_to_str(num)
print(str_num)  

# Method 3: String formatting
str_num = "%d" % num

# Method 4: f-strings
str_num = f"{num}"

# Method 5: The format() method
str_num = "{}".format(num) 

2. Use str() to Convert Integer to String in Python

The str() function is used to convert Integer to String in Python, actually this method can be used to convert any object to a string representation. When you use this method with an integer argument, it will return a string representation of the integer. The syntax for using str() to convert an integer to a string is simple:


# Using str() 
str_num = str(integer)

See the below example:


# Convert an Integer to a String
num = 465
str_num = str(num)
print(str_num)  

# Output: 
# "465"

3. chr() to Convert Integer to String

In Python, the chr() function also returns the string representing a character whose Unicode code point is the integer passed to it. We can use this function to convert an integer to a string by first adding the integer to the ASCII code of the character ‘0’, which gives us the ASCII code of the character representing the digit. We can then pass this ASCII code to the chr() function to get the corresponding character.

I am considering this method a classic and traditional, and not reommanded to use this method for just doing the normal conversion of an integer to a string.


# Function using traditional method.
def int_to_str(num):
    str_num = ""
    for d in str(num):
        str_num += chr(ord('0') + int(d))
    return str_num

# Example 
num = 345
str_num = int_to_str(num)
print(str_num)  

# Output: "345"

4. String Formatting

Python provides the % operator for string formatting, which you can use to convert an integer to a string by specifying the integer as part of a format string.


# Integer to string using string formatting
num = 231
str_num = "%d" % num
print(str_num)  

# Output: 
# "231"

We use the % operator to format the string "%d", which specifies that the argument should be formatted as an integer. We then pass the integer num as an argument to the % operator to get the resulting string.

5. f-strings – int to str Conversion

F-strings are a newer feature in Python 3 that provides a concise and readable way to format strings. We can use f-strings to convert an integer to a string by including the integer as part of the f-string.


# F-String method
num = 342
str_num = f"{num}"
print(str_num)  

# Output:
# "342"

We can also specify a format specifier inside the curly braces to control how the integer is formatted, similar to the format() method:


# Conversoin from int to str
num = 123
str_num = f"{num:05d}"
print(str_num)  

# Output:
# "00123"

6. format() – Conversion of Integer to String

This is just another way of formatting string. However we can use it to convert an integer to a string by specifying the integer as an argument to the method.


# Using the format() function
str_num = "{}".format(num)
print(str_num)  

# Output:
# "123"

Just like the f-string we can also specify a format specifier inside the placeholder to control how the integer is formatted:


# Using the format() function
str_num = "{:05d}".format(num)
print(str_num)  

# Output:
# "00123"

We include the format specifier :05d inside the placeholder, which specifies that the integer should be zero-padded to 5 digits. The resulting string is "00123".

7. Summary and Conclusion

In this article, you have learned how to convert an integer to string in Python. We have discussed different methods like str() and chr(), string formatting e.t.c. Since, there are more than one way to solve a problem, you need to choose the best once that fits for your need.

Happy Coding!