Python Int to String – Definitive Guide

In Python, numbers are handled as Integer objects. All integers are internally represented as long integer objects with the necessary arbitrary size. Integers are also called Int.

In python, you can convert int to String using the str() method.

You may need to convert the int to String in python for various reasons.

If You’re in Hurry…

You can convert Int to String using the below code snippet.

number = 77

try:
    # convert the number into string using str
    num_as_string = str(number)

    #Printing the number as string
    print("Number converted as String : " + num_as_string)

except ValueError:
    num_as_string = "Problem while converting number to string"

Output

Number converted as String : 77

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn different methods available to convert python int to String.

Using str() function

str() function can be used to convert numbers to String in Python. It accepts three parameters where

Syntax

str(object, encoding=encoding, errors=errors)
  • Object – A number object which needs to be converted to String
  • Encoding – An encoding charset which should be used to decode the number bytes as String
  • ErrorsError Handler. Example – strict to throw an error when a problem occurs. ignore to silently ignore the error and use the default replacement characters.

Example

number = 77

# print type of number variable
print(type(number))

try:
    # convert the number into string using str
    num_as_string = str(number)

    # print type of number variable after conversion
    print(type(num_as_string))

    #Printing the number as string
    print("Number converted as String : " + num_as_string)

except ValueError:
    num_as_string = "Problem while converting number to string"

Output

 <class 'int'>

 <class 'str'>

 Number converted as String : 77

By using str() method, you cannot pad zeros to the beginning of the String object.

If you want to pad zero to String, then you need to use the format() method explained in the further sections.

Concatenating strings and integers

If you want to concatenate Strings and Integer for printing, you can convert the number to String first using the Str() method and use the + symbol to concatenate with another String.

Example

vers = 3.8

info = "Python version is : " + str(vers)

print(info)

Output

Python version is : 3.8


These are the typical ways of using the Str() method to convert numbers to String in Python. Next, you’ll see the “%s” keyword to perform the conversion.

Using “%s” keyword

In this section, you’ll see how to convert Integer to String in Python using the ‘%s’ keyword.

This just formats the number as String rather than converting the number into a String. This usage is to use the %s as a placeholder and this will be replaced with the value available in the variable with the prefix %. You can use this method if you want to convert Python int to String without str() method.

Syntax

num_as_string = "%s" %number

Here it’ll replace the %s with the value available in the number variable.

Example

number = 77

# print type of number variable
print(type(number))

try:
    # convert the number into string using %s
    num_as_string = "%s" %number

    # print type of number variable after conversion
    print(type(num_as_string))

    #Printing the number as string
    print("Number converted to String using %s : " + num_as_string)

except ValueError:
    num_as_string = "Problem while converting number to string"

Output

<class 'int'>
<class 'str'>
Number converted to String using %s : 77

Formatting with Leading Zeros

If you want to format the number with leading zeros or if you want to pad additional zeros to the number, then you can use the %d along with the number of digits you need to display.

For example, if you use ‘%05d’, then additional zeros will be padded to the beginning of the number to make the total length of the String to 5.

You can make use of this method to convert integer to String with fixed length.

Example

number =77

print("Number converted to String using with leading zeros : " + '%05d' % number)

Output

Number converted to String using with leading zeros %s : 00077

This is how you convert Python numbers to strings using the %s and the %dkeyword.

Using .format() function

In this section, you’ll learn how to use the format() method available in python to convert Integer to String.

The format() method formats the string into the given representation. You can also use the placeholders in the format() method to replace the original value of the variable during formatting. You can use this method if you want to convert python int to string without str() method.

Syntax

num_as_string = "{}".format(number)

Example

number = 77

# print type of number variable
print(type(number))
try: 
    # convert the number into string using format
    num_as_string = "{}".format(number)

    # print type of number variable after conversion
    print(type(num_as_string))

    #Printing the number as string
    print("Number converted to String using Format : " + num_as_string)

except ValueError:
    num_as_string = "Problem while converting number to string"

Output

<class 'int'>
<class 'str'>
Number converted to String using %s : 77

Format with Leading Zeros

Using the format method, you can also pad additional zeros to the beginning of the number and make it a fixed length. For example, Length of 5.

Syntax

{:05d}'.format(number)

Example

number = 77
print("Number converted to String using Format and leading zeros : "  + '{:05d}'.format(number))

Output

Number converted to String using Format and leading zeros00077

Using Bind variables with Format Method()

You can use the bind variables in the format() method. The values of the variables will be replaced during the execution of the statement as shown in the example below.

langName = "Python"
version = 3.8

print("{langName} version is : {version} ".format(langName=langName, version=version))

Output

Python version is : 3.8 

This is how you can convert number to String format in Python. Next, you’ll learn how you can use f-String to perform the conversion.

Using f-string

Python f-Strings are introduced in version 3.6.

You can check the Python version using the below script.

Script

import sys
print(sys.version)

Output

3.8.2 (default, Sep  4 2020, 00:03:40) [MSC v.1916 32 bit (Intel)]

If your python version is greater than 3.6, then you can use the f-String method to convert integer to String.

f-String method is also known as literal String interpolation. This method provides a convenient and easy way to embed python expression inside string literals for formatting. This is the cleanest way of converting numbers to String in Python.

You can create an f-String by prefixing the letter f of F in the String.

Syntax

val = f'Stack Vidhya'

Example

number = 77

# print type of number variable
print(type(number))
try: 
    # convert the number into string using f-string
    num_as_string = f'{number}'

    # print type of number variable after conversion
    print(type(num_as_string))

    #Printing the number as string
    print("Number converted to String using f-string : " + num_as_string)

except ValueError:

    num_as_string = "Problem while formatting number to f-string"

Output

<class 'int'>
<class 'str'>
Number converted to String using f-string : 77

Using f-String to Pad Additional Zeros

You can convert number to String with Additional zeros by using the f-String method.

By using this method, you can specify the length of the number and the zeros will be padded to make the String length as specified.

Syntax

num_as_String = f'{number:05}'

Example

number = 77

print("Number converted to String using f-string with leading zeros : "  + f'{number:05}') # Preferred method, python >= 3.6

Output

Number converted to String using f-string with leading zeros : 00077

Using Bind Variables with f-String

You can also use the Bind variables in f-String. The actual value of the bind variables will be used during the execution of the program.

Example

langName = "Python"
version = 3.8

print(f"{langName} version is {version}")

Output

Python version is 3.8

This is how you can use bind variables in f-String.

Conclusion

To summarize, Converting Int to String is python is one of the important and widely used functions. You’ve seen the various ways available to convert Number to String with padding additional zeros while converting and also learned using the bind variables in the methods.

In every method available, using the f-String method is the cleanest method to convert number to String if your python version is greater than 3.6. Otherwise, you can use the str() method.

This conversion of variables from one type to another data type is also known as casting.

Feel free to comment, if you have any questions.

Leave a Comment