all Python 3Byte sequencesfunction
bytes.zfill
bytes — padding
Pad on the left with zeros to a given length.
Syntax
b.zfill(...)
A leading plus or minus stays in first position and the zeros are inserted after it — that is exactly what separates zfill from padding with zeros via rjust. Only one such sign byte at the very start is recognised; anything else, including a space or a 0x prefix, counts as ordinary content. A value already at least width bytes long is returned as is, never truncated.
Examples
print(b'42'.zfill(5)) # → b'00042'
print(b'-42'.zfill(5)) # → b'-0042'
print(b'+7'.zfill(6)) # → b'+00007'
print(b'12345'.zfill(3)) # → b'12345'
print(b'7'.zfill(3) + b'.jpg') # → b'007.jpg'
print(b''.zfill(2)) # → b'00'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy