How to Pad a String with Zeros in Python?

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:

02901

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

Pad a String with Zeros in Python

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:

008419

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

How to Pad a String with Zeros in Python

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:

04

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

Pad a String with Zeros in Python format string

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:

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.