How to get a string left padded with zeros in Python?


A string is a group of characters that can represent a single word or a whole sentence. In Python there is no need to declare strings explicitly we can directly assign them to a literal. Therefore, easy to use them.

A string is an object of the String class, which has multiple methods to manipulate and access strings.

In this article, we are going to find out how to get a string left padded with zeros in Python.

Using the rjust() function

One way to achieve this is using the built-in string function named rjust(). The width argument is the number of spaces you wish to fill (this number includes the length of the string.

If the number is less than the length of the string, no change is done), and the fillchar parameter is an optional parameter that fills the gaps with the supplied character (if no character is specified, spaces will be padded). To fill in or pad the spaces to the left of a string, use the rjust() function.

Example 1

In the program given below, we are using the rjust method for left padding the given string with zeroes. ‘0.

str1 = "Welcome to Tutorialspoint" str2 = str1.rjust(30, '0') print("Padding the string with zeroes ",str1) print(str2)

Output

The output of the above program is,

Padding the string with zeroes Welcome to Tutorialspoint
00000Welcome to Tutorialspoint

Example 2

In the example given below, we are using rjust and padding the left side of the string with the symbol “0”

str1 = "this is a string example....wow!!!"; print(str1.rjust(50, '0'))

Output

The output of the above given program is,

000000000000000000this is a string example....wow!!!

Using the format() function

Another alternative is to use the format() function. The string format approach may be used to fill in gaps and pad the string. On a print statement, the format() function is frequently used.

We'll utilize a colon to indicate the number of gaps in a curly brace that need to be filled in order to give the correct padding. We should additionally use the > sign to add left padding.

Example

In the example given below, we are taking a string as input and we are using the format method to left pad the string with zeroes.

str1 = "Welcome to Tutorialspoint" str2 = ('{:0>35}'.format(str1)) print("Left Padding of the string with zeroes ",str1) print(str2)

Output

The output of the above example is,

('Left Padding of the string with zeroes ', 'Welcome to Tutorialspoint')
0000000000Welcome to Tutorialspoint

Using the zfill() function

You can also left pad a string in Python with zeros using the zfill() function. We just have to specify the number of characters that you want to fill in with zeroes as parameter. This method returns a string with the given number of strings as output.

Example

In the example given below, we are taking a string as input and using the zfill method we are left padding the string with zeroes

str1 = "Welcome to Tutorialspoint" str2 = str1.zfill(35) print("Left Padding of the string with zeroes ",str1) print(str2)

Output

The output of the above example is,

('Left Padding of the string with zeroes ', 'Welcome to Tutorialspoint')
0000000000Welcome to Tutorialspoint

Updated on: 19-Oct-2022

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements