The Python zfill() method fills the string left with zero. The original length of the string is returned if the width is shorter than the string length.
The syntax of the method is: str.zfill(width)
Key Points :
• Return Type: Returns a string with the necessary zeroes in the primary string.
• The zfill() method takes one numerical parameter to determine which 0 is to be added.
• This method returns the copy of the string with 0 on the left. The length of the string returned depends on the length passed as a parameter.
• This function always returns a string.
Python String zfill(): Parameters
The description of parameters of zfill() method is given below:
• length: It is the only zfill() function parameter. This parameter defines the string length we want to construct. The value of this attribute must be greater than the actual length of the string to see any changes in the string.
Below is the python program to demonstrate the zfill() function:
str = "The Best Learning Resource For Online Education"; print (str.zfill(40)) print (str.zfill(50))
When we run above the program, the outcome is as follows:
The Best Learning Resource For Online Education
000The Best Learning Resource For Online Education
Below are several other functions that we can use to work with string in Python 3