How to Convert Integer into String in Python?

In Python, int or integer are referred to as whole number values or any value without a decimal point. The integer values can be positive or negative and are used for inputting data. Similarly, the string value represents any sequence of numbers, characters, or alphabet placed inside the single or double quotation marks.

Sometimes we need to enter integer data as a string or the required integer value inside the string. To deal with such scenarios, multiple built-in python functions can be used.

This write-up will give you a detailed understanding of converting integers into strings in Python using the following methods:

Now let’s get started!

How to Convert Integer into String in Python?

To convert integers into strings, the inbuilt “str()” function is commonly used in Python. The str() function is not the only way of integer-to-string conversion; instead, python provides some other methods, such as the “f-string” method and “.format()” function, and string formatting methods are used to convert integers into strings.

Let’s look at the first method with an example:

Method 1: Using str() Function

In the example given below, the inbuilt “str()” function is used to convert the integer value into a string.

Code:

#create integer
number = 99

print('Input Integer Value: ',number)
#printing data type of input variable
print("\nData Type of input variable: ", type(number))

#using str()
string_num = str(number)

print('\nConverted Integer Value into String: ',number)
#printing data type after conversion
print("\nData Type After conversion : ",type(string_num))

In the above code, the inbuilt “str()” function takes the integer variable as an argument and returns the string. The “type()” function is used to check the data type of the given number before and after the integer-to-string conversion.

Output:

A “str()” function successfully converted the integer value into a string in the above output.

Method 2: Using the f-string Method

In the example below, the “f-string” method converts integer values into strings. This method takes any data type value and returns it into string type.

Code:

#create integer
number = 32

print('Input Integer Value: ',number)
#printing data type of input variable
print("\nData Type of input variable: ", type(number))

# convert the num into string
string_val = f'{number}'

print('\nConverted Integer Value into String: ',string_val)
#printing data type after conversion
print("\nData Type After conversion : ",type(string_val))

In the above code, the “f-string” method takes the integer variable inside the curly braces and converts the given value into a string.

Output:

As seen in the output above, the integer has been converted into a string.

Method 3:Using .format() Function

In the following example, the “.format()” function is used to convert the integer value into a string.

Code:

#create integer
number = 122

print('Input Integer Value: ',number)
#printing data type of input variable
print("\nData Type of input variable: ", type(number))

# convert the integer into string
string_val = "{}".format(number)
#printing data type after conversion
print("\nData Type After conversion : ",type(string_val))

In the above code, the “.format()” function inserts the given integer value inside the blank string using curly braces “{ }”. An integer value is passed as a parameter to the “format()” function; consequently, it will convert the given value to a string.

Output:

The integer value has successfully converted into a string in the above output.

Method 4: Using the “%s” Operator

In the following example, the string formatting method is used to insert the integer value into a string and return a string as output.

Code:

number = 99

print('Input Integer Value: ',number)
#printing data type of input variable
print("\nData Type of input variable: ", type(number))
 
# using "%s" operator
conv_string = "% s" % number
print('\nConverted String Value: ',conv_string)
#printing data type after conversion
print("\nData Type After conversion : ",type(conv_string))

In the above output, an empty string is initialized with the “%s” operator. The input integer value which needs to be converted is placed after another “%” symbol, just as shown in the below snippet:

Output:

The integer value has been converted into a string using the “%s” operator.

That’s all from this blog post!

Conclusion

To convert integers into strings, the inbuilt “str()” function, “.format()” function, “f-string” method and “%s” operator are used in Python. The inbuilt “str()” function takes the integer as an argument and returns the string. Similarly, the “.format()” function and the “f-string” method also convert an input integer value to a string. The “%s” operator also works similarly to the previous two methods and places an integer inside the string value. This article presented how to convert integer values into strings in Python with numerous examples.