Python bytes.zfill() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

bytes.zfill(width) returns a copy of the data padded on the left with ASCII zero bytes (b"0", value 48) until it is width bytes long.

If the data starts with a sign byte, b"+" or b"-", the zeros are inserted after the sign, so b"-42" becomes b"-0042". If the data is already at least width long, it is returned unchanged.

bytes.zfill() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.zfill(...). No import is needed, and bytearray objects have the same method.

Syntax

Pass the total width you want.

python
result = data.zfill(width)
CallWhat it does
b"7".zfill(3)Returns b'007'.
b"-42".zfill(5)Returns b'-0042'.
b"12345".zfill(3)Returns b'12345': never truncates.

zfill() does not check that the data is numeric; it pads any bytes.

Method signatures
python
bytes.zfill(width)
# returns a new bytes object (bytearray.zfill() returns a bytearray)

Parameters

ParameterDescription
widthThe minimum length of the result in bytes.

Return Value

Return ValueDescription
bytesA copy padded with ASCII zeros on the left (after any sign) to width bytes.

Python Examples

Example 1 — Pad Counters and Signed Values

Pad a small counter, a negative and a positive signed value, and a value that is already long enough.

python
print(b"7".zfill(3))
print(b"-42".zfill(5))
print(b"+5".zfill(4))
print(b"12345".zfill(3))

How It Works

  1. Left padding. Two zero bytes bring b"7" up to three bytes.
  2. Sign stays first. The minus sign is kept at the front and the zeros go between it and the digits.
  3. Plus works too. A leading + is treated the same way.
  4. No truncation. A value longer than width comes back unchanged.

Example 2 — Build Fixed-Width ID Fields

Some text-based protocols expect numbers in fixed six-byte fields. Convert each int to ASCII bytes and pad it.

python
ids = [5, 42, 1234]

for n in ids:
    field = str(n).encode("ascii").zfill(6)
    print(field, len(field))

How It Works

  1. int to bytes. str() gives the digits and encode("ascii") turns them into bytes.
  2. Constant width. Every field ends up exactly six bytes long, which makes records easy to slice later.
  3. Different padding. Shorter numbers simply receive more zero bytes.

Example 3 — Pad Bit Strings and Edge Cases

Turn a number into an 8-bit pattern, then see how zfill() treats letters, empty bytes, and a lone sign.

python
bits = bin(5)[2:].encode("ascii")

print(bits.zfill(8))
print(b"ab".zfill(4))
print(b"".zfill(2))
print(b"-".zfill(3))

How It Works

  1. Full byte. bin() gives '0b101'; dropping the prefix and padding to 8 shows all eight bits.
  2. Not just digits. Letters are padded exactly like numbers.
  3. Empty input. Empty bytes become two zero digits.
  4. Lone sign. Even a bare minus stays in front of the padding.

Frequently Asked Questions

The ASCII digit zero, b"0" (value 48), not the null byte b"\x00".
A leading - or + stays first and the zeros are inserted after it: b"-7".zfill(4) is b"-007".
No. If the data is already width bytes or longer, it is returned unchanged.
rjust() puts the zeros before a sign (b"00-7"); zfill() keeps the sign in front (b"-007").
Use data.rjust(width, b"\x00") or data.ljust(width, b"\x00").

Back to the method index

See all 42 bytes method guides grouped by what they do.

Method Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful