How to Display Numbers with Leading Zeros in Python?

Display Numbers Wtih Leading Zeros

When ‘x’ number of zeros are added to the right of any number, it automatically increases the value of that number by ‘x’ times. But there might be instances in working with data which require one to include ‘x’ number of zeros to the left of a given number, which literally does nothing to the value of the number. It does not have any effect! But the requirement might be so specific that your intended results will not be arriving until this change is made.

To add leading zeros to numbers in Python, you can use the format() function, f-string technique, rjust() function, or zfill() function. Each method allows you to specify the total number of digits in the output, automatically adding the necessary zeros to the left of the number to meet the desired width.

Let’s get started!


Method I – Using the format( ) function

The number of zeros that are to be added preceding the number shall be mentioned in combination with the format( ) function. The logic behind this is that this function shall insert the required number of fields preceding the number. Since our requirement here is zeros, the same shall be coded as shown below.

print("{:06d}".format(99))
print("{:09d}".format(7))
## Output
000099
000000007
Leading Zeros Added Using Format Function
Leading zeros added using format( ) function

Upon closer observation, one can find that the 06d or 09d indicate the number of digits that are to be returned. The input number within the braces of the format function will be compared against the required number of digits & any difference shall be filled in the form of zeros preceding that number. So, it is imperative that if the count of required digits does not exceed the count of digits of the input number, no leading zeros will be added.


Method II – Using the f-string technique

An alternative to the above method is the f-string technique which requires the number to which the leading numbers are to be added, placed within the double quoted braces alongside the total number of digits required to be returned. Better shown than said, so here it is!

print(f"{67 :07d}")
print(f"{7 :03d}")
## Output
0000067
007
Leading Zeros Added Using F String
Leading zeros added using f-string

One can also use variables rather than direct numbers within the f-string such as the one given below.

a = 37
print(f"{a :07d}")
## Output
0000037
Leading Zeros Added Using A Variable
Leading zeros added using a variable

Method III – Using the rjust( ) function

This method uses a combination of the str( ) function along with the rjust( ) function to add leading zeros to any given number. The rjust( ) function would normally be used to return a string which is right justified. In this case, it is a number. Also, one ought to include the default entity that is to be used to fill the remaining digits, which in this case is zero. The following code demonstrates its usage.

str(73).rjust(7,'0')
str(23).rjust(5,'0')
Leading Zeros Added Using Rjust Function
Leading zeros added using rjust( ) function

Method IV – Using the zfill( ) function

The zfill( ) function also makes use of similar logic to fill the difference between the required number of digits and the number of digits in the input number with leading zeros. A striking difference in this technique is to include the input number within single quotes (‘ ‘), unlike the previous techniques. The following code demonstrates how to use the zfill( ) function to add the leading zeros.

'37'.zfill(6)

a = '37'
a.zfill(6)
Leading Zeros Added Using Zfill Function
Leading zeros added using zfill( ) function

Conclusion

Now that we have reached the end of this article, hope it has elaborated on the different techniques that can be deployed to add leading zeros to numbers in Python. Here’s another article that details the conversion of data type from float64 to int64 in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est facere!


Reference