Format integer into fixed length string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

Format integer into fixed length string in Python #

Use a formatted string literal to format an integer into a fixed length string, e.g. result = f'The number is {my_integer:03d}'. Formatted string literals let us include expressions inside of a string by prefixing the string with f.

main.py
my_integer = 8 # ✅ format integer into fixed length string (f-string) result = f'The number is {my_integer:03d}' print(result) # 👉️ The number is 008 my_integer = 8 # ------------------------------------------------ # ✅ format integer into fixed length string (with leading spaces) result = f'{my_integer:3}' print(repr(result)) # 👉️ ' 8' # ------------------------------------------------ # ✅ format integer into fixed length string (str.zfill()) result = str(my_integer).zfill(3) print(result) # 👉️ '008'

The first example uses a formatted string literal to format an integer into a fixed length string.

main.py
my_integer = 8 result = f'The number is {my_integer:03d}' print(result) # 👉️ The number is 008
The :03d syntax is used to pad the integer with leading zeros to a fixed width of 3 digits.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
hours = 8 minutes = 24 seconds = 3 result = f'The time is: {hours:02d}:{minutes:02d}:{seconds:02d} {"pm" if hours > 12 else "am"}' print(result) # 👉️ The time is: 08:24:03 am

You can use the same approach if you need to format the integer into a fixed length string with leading spaces.

main.py
my_integer = 8 result = f'{my_integer:3}' print(repr(result)) # 👉️ ' 8'

The integer in the example consists of a single digit, so it gets left-padded with 2 spaces.

Alternatively, you can use the str.zfill() method.

Format integer into fixed length string using str.zfill() #

To format an integer into a fixed length string:

  1. Use the str() class to convert the string to an integer.
  2. Use the str.zfill() method to format the integer into a fixed-length string.
main.py
my_integer = 8 result = str(my_integer).zfill(3) print(result) # 👉️ '008'

The str.zfill method takes the width of the string and left-fills the string with 0 digits to make it of the specified width.

main.py
my_integer = 10 result = str(my_integer).zfill(3) print(result) # 👉️ '010' result = str(my_integer).zfill(4) print(result) # 👉️ '0010'

Converting the number 10 to a string gives us a string with a length of 2.

Passing 3 as the width to the zfill() method means that the string will get left-filled with a single 0 digit.

The str.zfill() method handles a leading sign prefix (e.g. + or -) by inserting the padding after the sign.

main.py
my_integer = -10 result = str(my_integer).zfill(3) print(result) # 👉️ '-10' result = str(my_integer).zfill(4) print(result) # 👉️ '-010'

Note that the sign counts towards the width of the string.

If the specified width is less than or equal to the length of the original string, then the original string is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.