Bytearray Methods

bytearray.zfill()

Pad on the left with ASCII zero bytes until the requested width is reached. If the first byte is an ASCII plus or minus sign, insert zeros immediately after it. No numeric parsing or validation occurs. Widths below the current length preserve the data. Returns a new bytearray.

Formal Syntax & Structure

python syntaxpython
data.zfill(width, /)

Parameters & Attributes

NameTypeDescription
widthintMinimum length in bytes.

Return Value

bytearray

bytearray padded with ASCII zeros, respecting an initial ASCII sign.

Working Example

Example Snippetpython
print(bytearray(b'42').zfill(5))print(bytearray(b'-42').zfill(5))print(bytearray(b'+7').zfill(4))
Python Reference