What is zfill() method in Python?

PythonServer Side ProgrammingProgramming

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'
raja
Published on 19-Dec-2017 15:15:13
Advertisements