Python

Python Pad a String with Leading Zeros

Printing and storing numbers with leading zeros are important aspects as they significantly help in precisely transforming a number. This guide deliberates the different methods you can run to show a number that contains leading zeros in Python. You will learn how to pad a numeric or character string with zeros to a specific string length. The example codes also discuss how the numbers can be transformed to an organized String with leading 0’s.

String padding means placing some characters, and 0’s to the string to extend the string length to the specified length. There are two types of padding. The first one is left padding and the second one is right padding. The left padding means to add 0’s to the left side of the string, and the right name suggests that placing 0’s to the right side of the string. Python makes a programmer’s life easier by providing a variety of built-in functions. Come let’s briefly explain the padding process via example codes using Spyder Compiler in Windows 10. Below we are going to explain the zfill() and ljust() method with example codes.

Example 1: zfill() Method

For padding a string with leading zeros, we use the zfill() method, which adds 0’s at the starting point of the string to extend the size of the string to the preferred size. In short, we use the left padding method, which takes the string size as an argument and displays the string with the padded output. The command can only pad 0’s to the python string. So, here we go to implement our first program code with the help of the Windows 10 Spyder Compiler.

We create and initialize a “Python” string in the first code segment and use a print method to display the python string. In the next step, we call the zfill() command, which takes the string length as a parameter and then displays the string with leading zeros. The size of the string is ‘8’ with four 0’s at the beginning of the python string.

Note: The string mentioned in the sample code can be altered as well.

new_str = 'python'

print("Original string is : ",new_str)

pad_string = new_str.zfill(8)

print("Padded string is : ",pad_string)


It’s time to save and dry run the code to view how python pad a string with leading zeros. The output is attached in the below-affixed image.

Example 2: ljust() Method

To pad a string with leading zeros, we use another method, ljust() which adds 0’s at the ending point of the string; in short, right pad a python string. Here width is the original string size, and ‘fillchar’ is the character that pads a python string. If the char is not specified, then it gets ‘space’ as the default character. The method left pads the string with the specified character until the string size is “=” to the specified width.

So, here we go to implement our first program code via Spyder code file. We create and initialize a “Program” string in the first code segment and use a print method to display the python string. In the next step, we call the ljust() command, which takes two arguments as a parameter and then displays the string with leading zeros.

Note: The string mentioned in the sample code can be altered as well.

new_str = 'program'

print("Original string is : ",new_str)

rpad_string = new_str.ljust(9, '0')

print("New string is: ",rpad_string)


Again, save and dry run the code to view how python right pad a string with leading zeros. The output is attached in the below-affixed image.

Note: Keep in mind, in all the aforementioned methods, if the string width is less than the size of the actual string, then the resulting string is the actual string.

Conclusion:

In this tutorial, you learned different methods and ways to pad python strings with leading zeros. If you prefer the left padding method, zfill() is the simplest and unique method. If you want to pad different characters, you can use the rjust() and ljust() methods. Other than these methods, you can also give a try to format() or center() method. The python built-in methods are much easier to understand, code, and implement. I hope these two methods will be enough for your understanding.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content