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:
00902I have executed the above example code and added the screenshot below.

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:
00042I have executed the above example code and added the screenshot below.

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:
00001234I have executed the above example code and added the screenshot below.

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:
000789Here, 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:
0123456782. 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:
00098765433. 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:
000345Check 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:
- How to Round Numbers in Python?
- How to Check if a Number is Even or Odd in Python?
- How to Generate Random 4-Digit Numbers in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.