As a data scientist working with US census data, I recently encountered an issue where I needed to format ZIP codes consistently. To standardize them, I had to pad the shorter ZIP codes with leading zeros to ensure they all had a length of 5 digits. After researching different methods, I found four effective solutions. I will help you learn how to pad a string with zeros in Python In this tutorial with examples.
Pad a String with Zeros in Python
Python provides various ways to achieve this task. Let us learn some important methods.
Check out How to Find the Last Occurrence of a Substring in a String Using Python?
1. Use the zfill() String Method
Python’s built-in method zfill() is the most simple way to pad a string with zeros. It pads the string with zeros on the left until it reaches the specified width. Here’s an example:
zip_code = "2901"
padded_zip = zip_code.zfill(5)
print(padded_zip) Output:
02901I have executed the above example code and added the screenshot below.

In this example, calling zfill(5) on the string “2901” pads it with a leading zero to give it a total length of 5 characters.
Read How to Iterate Through a String in Python?
2. Pad Integers with Zeros
If you have an integer that you need to pad with zeros, you first need to convert it to a string. You can achieve this using the str() function combined with zfill():
population = 8419
padded_population = str(population).zfill(6)
print(padded_population) Output:
008419I have executed the above example code and added the screenshot below.

Here, we convert the integer 8419 to a string, then pad it with leading zeros to a width of 6 digits.
Check out How to Insert a Python Variable into a String?
3. Format Strings with Leading Zeros
Another approach is to use Python’s string formatting capabilities. You can use the % operator or the format() method to pad strings with zeros.
Using the % operator:
state_code = "4"
formatted_code = "%02d" % int(state_code)
print(formatted_code) Output:
04I have executed the above example code and added the screenshot below.

Read How to Split a String into Equal Parts in Python?
4. Use the format() Method
The format() method in Python provides a direct way to format strings, allowing you to specify the minimum width, alignment, and padding of numbers.
city_code = "271"
formatted_code = "{:03d}".format(int(city_code))
print(formatted_code) # Output: "271"In these examples, %02d and {:03d} specify that the integer should be formatted as a decimal with a minimum width of 2 and 3, respectively, padded with zeros.
Check out How to Create a String of N Characters in Python?
Conclusion
In this tutorial, I have explained how to pad a string with zeros in Python. I discussed methods such as using the zfill() string method, pad integers with zeros, strings with leading zeroes, and using the format() method.
You may also like to read:
- How to Compare Strings in Python?
- Find the First Number in a String in Python
- How to Reverse a String 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.