Pad a string with leading zeros in Python

Today we will discuss how can we pad a string with leading zeroes. Padding a string means adding some zeroes or any other character to the string to increase the length of the string to the desired length. For example, consider that we are given a string, ‘a’  and we have to increase its length to three, hence we can pad the string by adding zeroes to it. This will give us the new string, ’00a’ of length three.
There are two types of padding possible, they are:

  1. Left padding: Here we add the zero or the given character to the left of the string.
  2. Right padding: Here we add the zero or the given character to the right of the string.

In Python, we have two methods to pad a given string. They are as follows:

1. The zfill() method in Python

The zfill() method adds zeroes at the starting of the string(left padding) to increase the string length to the desired size. It takes the length of the string as a parameter and outputs the string with the padded results. This function can only pad zeroes to the string. An example of this is:

string = 'code'
print("Original string is : ",string)
new_string = string.zfill(8)
print("New string is : ",new_string)

Output:

Original string is : code
New string is : 0000code

In the above code, we have passed 8 as the argument to the zfill() function, hence the function outputs a string of length 8 by adding four zeroes at the beginning of the string.

2. rjust() method in Python

This is another method used to left pad a string. The syntax for this function is as follows:
String.rjust( width [, fillchar])

Here width is the required string length and fillchar is the character which is us to pad a string. If the character is not mentioned then it takes ‘space’ as the default character. This function left pads the string with the given character until the length of the string becomes equal to the width mentioned. An example of this function is:

string = 'python'
print("Original string is: ",string)
new_string = string.rjust(10, '0')
print ("New string is: ",new_string)

Output:

Original string is: python
New string is: 0000python

In the above code, we called the function rjust() for the given string, ‘python’ and passed 10(length of the required string) and ‘0’ (character) as the two parameters. As a result, we get the output as a string with length 10.

We can also pad a string using some other characters like

string = 'code'
print("Original string is: ",string)
new_string = string.rjust(6, '-')
print("New string is: ",new_string)

Output:

Original string is: code
New string is: --code

Right padding in Python:

We can right pad a string by adding a character at the end of the string to increase the length of the string till the required length. It is done by the function ljust(). It is similar to the rjust() function and has similar parameters. The syntax of ljust() function is as follows:

string.ljust(width [, fillchar])
where width is the required length of the string and fillchar is the character to be padded. An example for the same is:

string = 'code' 
print("Original string is: ",string) 
new_string = string.ljust(6, '0') 
print("New string is: ",new_string)

Output:

Original string is: code
New string is: code00

Important note: In all the functions mentioned above, if the width of the string which is passed as an argument to the function is less than the length of the original string then the function outputs the original string as the result.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *