What is zfill() method in Python?


The zfill method is built for left padding zeros in a string. For example:

>>> '25'.zfill(6)
'000025'

We can also use the rjust(width[, fillchar]) method in string class that right justifies the string and pads the left side with given filler char. The default filler char is space but we can provide it '0' as well. You can use it as follows:

>>> '25'.rjust(6, '0')
'000025'

We can also use Python string formatting to achieve this result as follows:

>>> print "%06d" % 25
'000025'

Updated on: 30-Sep-2019

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements