| by Arround The Web | No comments

Python Pad a String with Leading Zeros

A string is the object of the “String” class that is a collection of characters represented by a single word and the class has several methods to manipulate and access the strings. In Python, we don’t need to declare strings explicitly, we are allowed to directly assign them to a literal. Moreover, we can pad the string with numbers which can be needed for a few reasons. For instance, to make a numeric string for easy sorting, to make a number look more readable, and many more.

This post will talk about different methods for padding any string with leading zeros in Python.

How to Pad a String With Leading Zeros in Python?

Below provided methods are used to pad a string with leading zeros in Python:

Method 1: Pad a String With Leading Zeros in Python Utilizing “f-string” Method

The most recent version of Python provides a method “f-string” for quick string formatting. It can be used for padding a string with leading zeros in Python.

Example

Declare two string variables named “string1” and “string2”. Then, initialize with values:

string1 = 'Linux'

string2 = 'Hint'

Now, call the “print()” method with the “f-string” to pad the provided string with leading zeros. Here, the value “10” indicates that increase in the length of provided string by using zeros:

print(f'{string1:0>10}')

print(f'{string2:0>8}')

As you can see, our provided strings have the “5” and “4” length, which are now increased to “10” and “8” respectively by leading zeros:

Method 2: Pad a String With Leading Zeros in Python Utilizing “format()” Method

Another way to pad a Python string with all zeros is the “format()” method. The “format()” method can format any provided string to display them in our required format based on some patterns.

Example

Call the “format()” method inside the print statement and pass a desired string as an argument along with the required length of string which needs to replace with zeros:

print('{:0>10}'.format(string1))

print('{:0>8}'.format(string2))

Output

Method 3: Pad a String With Leading Zeros in Python Utilizing “zfill()” Method

For padding a string with leading zeros in Python, the “zfill()” method can be used. It takes a number providing the required length of any Python string as an argument and adds zeros at the left side of the string until it is of the specified length. In short, it adds the “0” at the left side of any provided string.

Example

Call the “zfill()” method and pass the desired number of string length inside the “print()” function and print its result:

print(string1.zfill(10))

print(string2.zfill(8))

Output

Method 4: Pad a String With Leading Zeros in Python Utilizing “rjust()” Method

If you want to pad a string with zeros only at the left side of any string until the string is of the provided length, the “rjust()” can be used.

Example

Use the “rjust()” method along with the required length of string and the number which needs to be added to make the string according to the desired length as an argument. Then, pass them to the “print()” method:

print(string1.rjust(10, "0"))

print(string2.rjust(8, "0"))

It can be observed that the provided string contains zeros at the left side of it:

Method 5: Pad a String With Leading Zeros in Python Utilizing “ljust()” Method

The “ljust()” method is used to add zeros at the right side of any string until the string is of the provided length. For a better understanding, check out the following implementation of this method.

Example

Call the “ljust()” method along with the required argument inside the “print()” method:

print(string1.ljust(10, "0"))

print(string2.ljust(8, "0")

Output

That’s it! We have compiled multiple methods for padding the Python string with leading zeros.

Conclusion

To pad the string with leading zeros in Python, the “f-string“, the “format()” method, the “zfill()” method, the “rjust()” method, and the “ljust()” method can be used. All specified methods are inbuilt methods of Python. This post described multiple methods to pad any Python string with leading zeros.

Share Button

Source: linuxhint.com

Leave a Reply