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

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

age_str = '{}'.format(age)

age_str = '{}'.format(40)

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}'

Leave a Comment