Convert Integer (int) 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 type 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.

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
  • %s Format Specifier
  • 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

str() Method Sytanx

The defacto way to convert any type of value into the 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.

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

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

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 %s format Specifier

Python provides the %s format specifier which is used to convert an integer into the string and use it inside a string. The %s format specifier is generally used with the print() method like below.

age = 40

print("My age is %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 = f'{age}'

age_str = f'{40}'

Convert and Print Integer with print() Method

The 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