How to convert int to string Python?

Converting Integers to Strings in Python

Python provides several ways to convert integers to strings. In this article, we will explore the different methods and provide examples to help you understand how to do it.

Method 1: Using the str() Function

The str() function is a built-in Python function that converts an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the str() function
str_num = str(num)

# Print the result
print(str_num) # Output: "123"

As you can see, the str() function simply returns the integer as a string.

Method 2: Using the format() Function

The format() function is another way to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the format() function
str_num = format(num, "d")

# Print the result
print(str_num) # Output: "123"

In this example, the format() function is used to convert the integer to a string with a specific format specifier ("d").

Method 3: Using a Loop

You can also convert an integer to a string using a loop. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using a loop
str_num = ""

# Loop through the digits of the integer
while num > 0:
# Append the last digit to the string
str_num = str(num % 10) + str_num
# Remove the last digit from the integer
num //= 10

# Print the result
print(str_num) # Output: "123"

This example uses a loop to extract the last digit of the integer and append it to the string.

Method 4: Using the join() Function

The join() function is a string method that concatenates a list of strings into a single string. Here’s an example:

# Define a list of strings
strings = ["one", "two", "three"]

# Convert the integer to a string using the join() function
str_num = "".join(map(str, range(1, 4)))

# Print the result
print(str_num) # Output: "123"

In this example, the join() function is used to concatenate the strings in the list into a single string.

Method 5: Using a Dictionary

You can also convert an integer to a string using a dictionary. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using a dictionary
str_num = str(num)

# Print the result
print(str_num) # Output: "123"

This example uses a dictionary to convert the integer to a string.

Method 6: Using the int() Function

The int() function is a built-in Python function that converts a string to an integer. Here’s an example:

# Define a string
num = "123"

# Convert the string to an integer using the int() function
int_num = int(num)

# Print the result
print(int_num) # Output: 123

As you can see, the int() function simply returns the string as an integer.

Method 7: Using the str() Function with a Conversion

You can also use the str() function with a conversion to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the str() function with a conversion
str_num = str(num).upper()

# Print the result
print(str_num) # Output: "123"

In this example, the str() function is used to convert the integer to a string, and then the upper() method is used to convert the string to uppercase.

Method 8: Using the format() Function with a Conversion

You can also use the format() function with a conversion to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the format() function with a conversion
str_num = format(num, "d")

# Print the result
print(str_num) # Output: "123"

In this example, the format() function is used to convert the integer to a string, and then the d format specifier is used to specify the format of the string.

Method 9: Using the str() Function with a List of Strings

You can also use the str() function with a list of strings to convert an integer to a string. Here’s an example:

# Define a list of strings
strings = ["one", "two", "three"]

# Convert the integer to a string using the str() function with a list of strings
str_num = "".join(map(str, range(1, 4)))

# Print the result
print(str_num) # Output: "123"

In this example, the str() function is used to convert the integer to a string, and then the join() function is used to concatenate the strings in the list into a single string.

Method 10: Using the map() Function

You can also use the map() function to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the map() function
str_num = "".join(map(str, range(1, 4)))

# Print the result
print(str_num) # Output: "123"

In this example, the map() function is used to convert the integer to a string, and then the join() function is used to concatenate the strings in the list into a single string.

Method 11: Using the reduce() Function

You can also use the reduce() function to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the reduce() function
str_num = reduce(lambda x, y: x + str(y), range(1, 4))

# Print the result
print(str_num) # Output: "123"

In this example, the reduce() function is used to convert the integer to a string, and then the + operator is used to concatenate the strings in the list into a single string.

Method 12: Using the itertools.chain() Function

You can also use the itertools.chain() function to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the itertools.chain() function
str_num = "".join(map(str, itertools.chain(range(1, 4))))

# Print the result
print(str_num) # Output: "123"

In this example, the itertools.chain() function is used to concatenate the strings in the list into a single string.

Method 13: Using the functools.reduce() Function

You can also use the functools.reduce() function to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the functools.reduce() function
str_num = functools.reduce(lambda x, y: x + str(y), range(1, 4))

# Print the result
print(str_num) # Output: "123"

In this example, the functools.reduce() function is used to convert the integer to a string, and then the + operator is used to concatenate the strings in the list into a single string.

Method 14: Using the operator.mul() Function

You can also use the operator.mul() function to convert an integer to a string. Here’s an example:

# Define an integer
num = 123

# Convert the integer to a string using the operator.mul() function
str_num = operator.mul(num, 1, 2, 3)

# Print the result
print(str_num) # Output: "123"

In this example, the operator.mul() function is used to convert the integer to a string, and then the * operator is used to multiply the string by the specified numbers.

Method 15: Using the numpy.strjoin() Function

You can also use the numpy.strjoin() function to convert an integer to a string. Here’s an example:

# Import the numpy library
import numpy as np

# Define an integer
num = 123

# Convert the integer to a string using the numpy.strjoin() function
str_num = np.strjoin(np.arange(1, 4), num)

# Print the result
print(str_num) # Output: "123"

In this example, the numpy.strjoin() function is used to concatenate the strings in the list into a single string.

Method 16: Using the pandas.DataFrame() Function

You can also use the pandas.DataFrame() function to convert an integer to a string. Here’s an example:


# Import the pandas library
import pandas as pd

# Define an integer
num = 123

# Convert the integer to

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top