all Python 3Byte sequencesfunction
bytearray.zfill
bytearray — padding
Pad on the left with zeros to a given length.
Syntax
ba.zfill(...)
The one thing zfill adds over rjust with a zero fill: a leading ASCII + or - stays in front and the zeros go after it. It is plain byte padding, not number formatting — b'3.5' or any non-digit content is padded just the same, and values longer than the requested width are returned unchanged.
Examples
print(bytearray(b'42').zfill(5)) # → bytearray(b'00042')
print(bytearray(b'-42').zfill(5)) # → bytearray(b'-0042')
print(bytearray(b'12345').zfill(3)) # → bytearray(b'12345')
print(bytearray(b'7').zfill(2) + b':' + bytearray(b'5').zfill(2)) # → bytearray(b'07:05')
print(bytearray(b'').zfill(3)) # → bytearray(b'000')
See also
Open in the full glossarydocs.python.orgReport an inaccuracy