Python String Zfill Method Example

  173 views   7 months ago Python

Python zfill() is an inbuilt python string method that is used to append zero to the left side of the given string. It is a padding mechanism. The number of 0’s to be appended decided by the argument, which is a number passed in the zfill() method. The argument is the total length of the list.  Suppose the current length of the string is 20, and the argument is 30, then the zfill() method will attach 10 zeroes to the leftmost side of the string to make that string of length 30. The zfill() function returns a string.

Python String zfill()

Python zfill() method fills the string at left with 0 digits to make a string of length width. It returns a string contains a sign prefix + or – before the 0 digits. It returns original length string if the width is less than string length.



Here str is the string variable that holds the main string, which is to be filled.

Length is the length of the string that we want to make.


It takes a single parameter, which is the length. It specifies the length of the string we want. It must be greater than the current length of the string.

Return Value

It returns a string with the required number of zeroes padded in the main string.

Example program on zfill() in python

Write a program to show the working of the zfill() method in python.


h1= "MilEy CirUS"
h2= "Hello Boy"
h3= "Qwerty"
h4= "HELLO girl"
h5="lionel messi IS THE BEST"



➜  pyt python3
000000000MilEy CirUS
000000Hello Boy
00000000000000HELLO girl
000000lionel messi IS THE BEST
➜  pyt

Hope you like this small article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]