How to Pad Numbers with Leading Zeros in Python?

In this tutorial, I will explain how to pad numbers with leading zeros in Python. Someone asked me this question during a Python webinar then I explored more about this topic by the end of this guide, you will have a solid understanding of various methods to achieve this in Python with examples and screenshots of executed example code.

Methods to Pad Numbers with Leading Zeros in Python

There are several ways to pad numbers with leading zeros in Python. We will explore some important methods:

Read How to Check if Input is a Number in Python?

1. Use the zfill() Method

The zfill() method is one of the simplest ways to pad a string with leading zeros. This method works by padding the string on the left with zeros until the desired length is reached.

Example: Format ZIP Codes

zip_code = "902"
padded_zip_code = zip_code.zfill(5)
print(padded_zip_code)  

Output:

00902

I have executed the above example code and added the screenshot below.

Pad Numbers with Leading Zeros in Python

In this example, the ZIP code “902” is padded to a five-digit string “00902” using the zfill() method.

Check out What are Floating Point Numbers in Python?

2. Use String Formatting with F-Strings

F-strings, introduced in Python 3.6, provide a concise and readable way to format strings. You can use f-strings to pad numbers with leading zeros by specifying the width and using the :0 format specifier.

Example: Format Employee IDs

employee_id = 42
padded_employee_id = f"{employee_id:05}"
print(padded_employee_id) 

Output:

 00042

I have executed the above example code and added the screenshot below.

How to Pad Numbers with Leading Zeros in Python

Here, the employee ID “42” is formatted as a five-digit string “00042” using an f-string.

Read How to Count the Number of Digits in a Number in Python?

3. Use the str.format() Method

The str.format() method is another versatile way to format strings in Python. You can achieve padding with leading zeros by specifying the width and using the :0 format specifier within the format() method.

Example: Format Account Numbers

account_number = 1234
padded_account_number = "{:08}".format(account_number)
print(padded_account_number)  

Output:

00001234

I have executed the above example code and added the screenshot below.

Pad Numbers with Leading Zeros in Python str.format() Method

In this case, the account number “1234” is padded to an eight-digit string “00001234” using the str.format() method.

Check out How to Split a Number into Digits in Python?

4. Use the rjust() Method

The rjust() method pads the string on the left with the specified character (in this case, ‘0’) until the desired length is reached. This method is useful when you need to ensure that the string reaches a specific width.

Example: Format Order Numbers

order_number = "789"
padded_order_number = order_number.rjust(6, '0')
print(padded_order_number)  

Output:

000789

Here, the order number “789” is padded to a six-digit string “000789” using the rjust() method.

Check out How to Check if a String is Alphabet in Python?

Applications

Let us see some practical applications to pad numbers with leading zeroes in Python.

1. Format Social Security Numbers

Social Security Numbers (SSNs) in the United States are typically formatted as nine-digit numbers. If you have an SSN stored as an integer, you can pad it with leading zeros to ensure it has nine digits.

ssn = 12345678  # Missing one digit
formatted_ssn = f"{ssn:09}"
print(formatted_ssn) 

Output:

012345678

2. Format Phone Numbers

Phone numbers in the United States often need to be formatted with leading zeros, especially when dealing with area codes. Here’s how you can pad a phone number to ensure it has 10 digits.

phone_number = 9876543  # Missing area code
formatted_phone_number = f"{phone_number:010}"
print(formatted_phone_number)   

Output:

0009876543

3. Format Invoice Numbers

In business applications, invoice numbers often need to be displayed with a fixed length for consistency. Here’s an example of how to pad invoice numbers with leading zeros.

invoice_number = 345
formatted_invoice_number = "{:06}".format(invoice_number)
print(formatted_invoice_number)   

Output:

000345

Check out How to Check if a String Represents a Number with Decimal in Python?

Conclusion

In this tutorial, I will explain how to pad numbers with leading zeros in Python. I explored the use of zfill() method, f-strings , the str.format() method, or the rjust() method, you can easily format your numbers to meet your specific needs. We also discussed some important applications to format social security numbers, phone numbers, and invoice numbers.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.