Format a number to a fixed width in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Format a number to a fixed width in Python #

Use a formatted string literal to format a number to a fixed width, e.g. result = f'{my_int:03d}'. The formatted string literal will format the number to the specified width by prefixing it with the necessary number of leading zeros.

main.py
my_int = 12 # ✅ format integer to fixed width of 3 result = f'{my_int:03d}' print(result) # 👉️ '012' # ✅ format integer to fixed width of 4 result = f'{my_int:04d}' print(result) # 👉️ '0012' # ✅ format integer to fixed width of 3 (with leading spaces) result = f'{my_int:3}' print(repr(result)) # 👉️ ' 12' # ----------------------------------- my_float = 1.234567 # ✅ format float to fixed width of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235' # ----------------------------------- # ✅ using str.zfill() result = str(my_int).zfill(3) print(result) # 👉️ '012' result = str(my_int).zfill(4) print(result) # 👉️ '0012'

The first example uses a formatted string literal to format a number to a fixed width.

main.py
my_int = 12 result = f'{my_int:03d}' print(result) # 👉️ '012' result = f'{my_int:04d}' print(result) # 👉️ '0012'

The first f-string formats the number to a width of 3 digits and the second to a width of 4 digits.

If necessary, leading zeros are added to format the number to the specified width.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'The number is:' my_int = 5000 result = f'{my_str} {my_int}' print(result) # 👉️ The number is: 5000

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

You can also use a formatted string literal if you need to format a float to a fixed width.
main.py
my_float = 1.234567 # ✅ format float to fixed length of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235'

The example formats the float to a width of 8 with 3 digits after the decimal.

The string gets left-padded with spaces to format the float to the specified length.

Format a number to a fixed width using str.zfill() #

To format a number to a fixed width:

  1. Use the str() class to convert the number to a string.
  2. Use the str.zfill() method to format the string to the specified width.
  3. The zfill() method left-fills the string with 0 digits to make it of the specified length.
main.py
my_int = 12 result = str(my_int).zfill(3) print(result) # 👉️ '012' result = str(my_int).zfill(4) print(result) # 👉️ '0012'

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.

Converting the number 12 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
num = -12 result_1 = str(num).zfill(3) print(result_1) # 👉️ '-12' result_2 = str(num).zfill(4) print(result_2) # 👉️ '-012'

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.