How to add leading Zeros to a Number in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. Add leading zeros to a number in Python
  2. The zfill() method handles the leading sign prefix
  3. Using a formatted string literal to add leading zeros to a number
  4. Add leading zeros to a number using str.format()
  5. Add leading zeros to a number using str.rjust()
  6. Add leading zeros to a number using format()

# Add leading zeros to a number in Python

To add leading zeros to a number:

  1. Use the str() class to convert the number to a string.
  2. Use the str.zfill() method to add leading zeros to the string.
  3. The method takes the width of the string and pads it with leading zeros.
main.py
num = 246 result_1 = str(num).zfill(5) print(result_1) # ๐Ÿ‘‰๏ธ '00246' result_2 = str(num).zfill(6) print(result_2) # ๐Ÿ‘‰๏ธ '000246'

add leading zeros to number

The code for this article is available on GitHub

We used the str() class to convert the number to a string.

This is necessary because adding leading zeros to a number causes a SyntaxError.

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
num = 13 result_1 = str(num).zfill(3) print(result_1) # ๐Ÿ‘‰๏ธ '013' result_2 = str(num).zfill(4) print(result_2) # ๐Ÿ‘‰๏ธ '0013'

Converting the number 13 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 zfill() method handles the leading sign prefix

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

main.py
num = -13 result_1 = str(num).zfill(3) print(result_1) # ๐Ÿ‘‰๏ธ '-13' result_2 = str(num).zfill(4) print(result_2) # ๐Ÿ‘‰๏ธ '-013'

zfill method handles leading sign prefix

The code for this article is available on GitHub

Note that the sign counts toward 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.

main.py
num = 13 result_1 = str(num).zfill(2) print(result_1) # ๐Ÿ‘‰๏ธ '13' result_2 = str(num).zfill(1) print(result_2) # ๐Ÿ‘‰๏ธ '13'

The number in the example has a length of 2, so trying to fill it to 2 characters doesn't have an effect.

# Using a formatted string literal to add leading zeros to a number

Alternatively, you can use a formatted string literal to add leading zeros to a number.

main.py
num = 13 result_1 = f'{num:04}' print(result_1) # ๐Ÿ‘‰๏ธ '0013' result_2 = f'{num:05}' print(result_2) # ๐Ÿ‘‰๏ธ '00013'

using formatted string literal to add leading zeros to number

The code for this article is available on GitHub
The first digit after the colon is the fill value, and the second is the width of the string.

We don't have to use the str() class to convert the integer to a string as the conversion is done for us automatically.

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

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐Ÿ‘‰๏ธ is subscribed: True

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

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

The first digit after the colon is the fill value and the second is the width of the string.

main.py
num = 13 result_1 = f'{num:04}' print(result_1) # ๐Ÿ‘‰๏ธ '0013'

This approach also works if the width of the string is stored in a variable.

main.py
num = 13 width_of_string = 4 result_1 = f'{num:0{width_of_string}}' print(result_1) # ๐Ÿ‘‰๏ธ '0013'

# Add leading zeros to a number using str.format()

You can also use the str.format() method to add leading zeros to a number.

main.py
num = 13 result_1 = '{:04}'.format(num) print(result_1) # ๐Ÿ‘‰๏ธ '0013' result_2 = '{:05}'.format(num) print(result_2) # ๐Ÿ‘‰๏ธ '00013'

add leading zeros to number using str format

The code for this article is available on GitHub
The first digit after the colon is the fill value and the second is the width of the string.

The str.format() method performs string formatting operations.

main.py
first = 'bobby' last = 'hadz' result = "Name: {} {}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "Name: bobby hadz"

The string the method is called on can contain replacement fields specified using curly braces {}.

# Add leading zeros to a number using str.rjust()

You can also use the str.rjust() method to add leading zeros to a number.

main.py
num = 13 result_1 = str(num).rjust(4, '0') print(result_1) # ๐Ÿ‘‰๏ธ '0013' result_2 = str(num).rjust(5, '0') print(result_2) # ๐Ÿ‘‰๏ธ '00013'

add leading zeros to number using str rjust

The code for this article is available on GitHub

The str.rjust() method pads the beginning of the string to the specified width with the provided fill character.

The str.rjust method takes the following 2 arguments:

NameDescription
widthThe total length of the padded string
fillcharThe fill character to pad the string with
main.py
num = 13 result_1 = str(num).rjust(4, '0') print(result_1) # ๐Ÿ‘‰๏ธ '0013'

The first argument is the width of the padded string and the second is the fill character (0 in our case).

Notice that we had to convert the number to a string using the str() class.

This is necessary because rjust() is a method implemented on strings.

# Add leading zeros to a number using format()

You can also use the format() function to add leading zeros to a number.

main.py
num = 13 result = format(num, '03') print(result) # ๐Ÿ‘‰๏ธ 013 result = format(num, '04') print(result) # ๐Ÿ‘‰๏ธ 0013 result = format(num, '05') print(result) # ๐Ÿ‘‰๏ธ 00013

add leading zeros to number using format

The code for this article is available on GitHub

The first argument the format() function takes is the value and the second is a string used to format the value.

The first digit is the fill character (0) and the second is the total width of the string.

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.

Copyright ยฉ 2024 Borislav Hadzhiev