Python String zfill()
In Python to append zeroes on the left side of a string
zfill() string method is used. Python string
zfill() method is an inbuilt method.
Basically it provides a padding mechanism, where only 0 character is used for padding. The Python center() method is also used for more or less this purpose.
zfill()method takes one numeric parameter which help in deciding the number of 0’s to be appended.
The argument indicates the total required length of the string. So in case the length of the string is less then it is padded with 0 numerals in the left, but if the length of the string is already equal or greater than the length parameter, then no padding is done.
This function always returns a string.
Suppose we have a string with current length as 30 and length argument passed is 40; then
zfill() method will append 10 zeros to the left side of the string and thus make string length equals to 40.
Below we have a basic syntax of
zfill() method in Python:
Note: In the above syntax, str denotes the string variable on which we apply
The description of parameters of
zfill() method is given below:
This is the only parameter of the
zfill()method. This parameter specifies the length of the string that we want to make. This attribute's value must be greater than the actual length of the string, to see any change in the string.
zfill(): Returned Values
It returns a string with the required number of zeroes appended in the main string.
zfill(): Basic Example
Below we have an example to show the working of the
zfill() string method in python:
str1="QWERTYUIOP" str2="STUDYTONIGHT" str3="no way!!" print(str1.zfill(20)) print(str2.zfill(5)) print(str3.zfill(4))
The Output will be:
If the passed argument's value or value of
length parameter is less than the actual string length then the original string is returned as it is.
Some Points to remember
This method returns the copy of the string with 0's appended to the left-hand side. The length of the string that will be returned is totally dependent upon the length that is passed as a parameter:
For example length of the string is 10 and width passed as an argument has value equals to 15. In this situation,the
zfill()method returns a string with five zeros appended to its left.
Let's take another example with the initial length of the string as 10. The width passed as an argument has value equals to 8. In this case, the string
zfill()doesn't fill '0' digits to the left and returns the original string. In this case, the returned string's length will be 10.
zfill(): Another Example
There is another example where input is taken from the user and then
zfill() method is applied to it. Let us see the code snippet given below:
for i in range(0,2): h=input() length=int(input()) print(h.zfill(length),"\n")
The Output will be:
Time for Live Example!
Below we have a Live Example of
zfill() method. Let us see the code snippet:
In this tutorial, we learned how to append zero on the left side of a string using
zfill() method of strings in Python.