Convert Integer To String In Python

Like all popular programming languages, Python provides the integer type in order to store integer values. The string is another popular data type and the integers can be converted into string types in different formats. Even the str() method is the well-known and most practical way there are different ways to convert an integer into a string. But there are other ways and methods in order to convert an integer into a string type.

Methods To Convert Integer To String

Python provides the following methods and ways in order to convert an integer variable of value into a string.

  • str() Method is the most popular way to convert integer to string.
  • %s Format Specifier is an alternative way to convert integer to string.
  • format() Method
  • f Formatter

Check Integer and String Type Before and After Conversion

Python provides different data types like integer, string, list, etc. Python also provides the type() method which can be used to check a given object, variable, or value type. While converting from integer to string the type() method can be used to check types.

print( type( 40 ) )

print( type( "40" ) )

age = 40

print( type( age ) )

age = "40"

print( type( age ) )
Check Integer and String Type

Convert Integer To String with str() Method

The defacto way to convert any type of value into a string is using the str() method. Even the parameter can be an integer, float, etc. in this case we are interested in converting from integer to string. The syntax of the str() method is like below.

str(OBJECT)
  • OBJECT can be a value that is an integer, float, etc. This parameter is required.

The most popular, reliable, and easy way to convert an integer into a string is to use the str() method. The str() method is provided by Python as a built-in method and there is no need to import a module. The syntax of the str() method is like below where the integer value or variable can be provided to the str() method as the parameter.

age = 40

age_str = str(age)

type(age_str)


age_str = str(40)

type(age_str)
Convert Integer (int) To String with str() Method

Convert Integer To String with __str__() Method

In the previous example, we used the str() method to convert an integer into a string. Under the hood, this method calls the integer value or variable __str__() method. For example in the following example calling the str(age) is equal to the age.__str__() .

age = 40

age_str = age.__str__()

type(age_str)
<class 'str'>

This means the age_str variable type is a string.

Convert Integer (int) To String with %s format Specifier

Python provides the %s format specifier which is used to convert an integer into a string and use it inside a string. The %s format specifier is generally used with the print() method like below. Actually, this is not a real conversion as the variable stays as an integer but is used inside a string like a string variable. But by using "%s" % age" we convert the integer age variable into a string variable named age_str.

age = 40

print("My age is %s" % (age))

age_str = "%s" % age

Convert Integer (int) To String with format() Method

The string type provides the format() method as built-in. An empty string can be used to call the format() method and provide the integer variable or value as a parameter. This will convert the given integer value or variable into a string.

age = 40

a = 5

age_str = '{}'.format(age)

age_str = '{}'.format(40)

five = "{}".format(5)

eleven = "{}".format(5+6)

fifty_six= "{}{}".format(5,6)

Convert Integer (int) To String with f Formatter

The f'{}’ formatter can be used to convert an integer into a string. This is a very similar way of using the format() method. The integer value or variable will be provided as a parameter inside the curly brackets.

age = 40

#age_str is a string type
age_str = f'{age}'

#age_Str is a string type
age_str = f'{40}'

Convert and Print Integer with print() Method

Print() is a very useful method used to print different data and variables into the standard output or standard error etc. Below we will print integer variables and values into the standard output or terminal by converting them into the string.

a = 5

b = 6

print("6 + 5 is equal to",6+5)

print(a,"+",b," is equal to",6+5)

print(f"{a} + {b} is equal to",6+5)

Leave a Comment