Python String zfill() Method

Short answer
str.zfill(width) pads a string on the left with zeros until it is width characters long, for example "42".zfill(5) gives '00042'.

Use zfill() whenever numbers must have a fixed number of digits: invoice numbers like 00042, file names like img_007.png, or clock times like 09:05. It is a string method, so convert integers with str() first. It is also sign-aware: a leading + or - stays at the front and the zeros go after it. For other padding characters use rjust(), and for fancy layouts see the format() method.

Quick facts
Syntaxtext.zfill(width)
Parameterswidth: the minimum length of the result
ReturnsA new string padded with 0 on the left
If width <= len(text)Returns the text unchanged
SignsA leading + or - stays first

How str.zfill() pads with zeros

The zeros are added on the left until the string has the requested width. Longer strings are never shortened.

Python
print("7".zfill(3))
print("123".zfill(3))
print("12345".zfill(3))
print("abc".zfill(6))

The last line shows that zfill() works on any string, not only on digits.

How zfill() treats plus and minus signs

If the string starts with + or -, the sign is kept in front and the zeros are inserted after it. That is what you want for numbers, and the reason zfill() differs from rjust(width, "0").

Python
print("-42".zfill(5))
print("+7".zfill(4))
print("-42".rjust(5, "0"))

zfill() vs rjust() vs f-string zero padding

ApproachCodeResultNotes
zfill()"42".zfill(5)00042String method; sign-aware
rjust()"42".rjust(5, "0")00042Any fill character, not sign-aware
f-stringf"{42:05d}"00042Works on the integer directly
format()"{:05d}".format(42)00042Same as the f-string
% style"%05d" % 4200042Older style

Examples you can run

Press Run, then edit the code and run it again.

Zero-pad an invoice number

Convert the number to a string, then pad it to six digits.

Zero-pad an invoice number
invoice = 42
code = "INV-" + str(invoice).zfill(6)
print(code)
Expected outputINV-000042

Format a clock time

Hours and minutes need two digits each.

Format a clock time
hours, minutes = 9, 5
print(str(hours).zfill(2) + ":" + str(minutes).zfill(2))
Expected output09:05

Build sortable file names

Padded numbers sort correctly as text, while unpadded numbers do not.

Build sortable file names
plain = ["img" + str(n) for n in (1, 2, 10)]
padded = ["img" + str(n).zfill(3) for n in (1, 2, 10)]
print(sorted(plain))
print(sorted(padded))
Expected output['img1', 'img10', 'img2']
['img001', 'img002', 'img010']

Pad a negative number

The minus sign stays first, and the zeros come after it.

Pad a negative number
print(str(-5).zfill(4))
Expected output-005

Common mistakes

Calling zfill() on an integer

✗ Wrong

n = 42
print(n.zfill(5))
AttributeError: 'int' object has no attribute 'zfill'

✓ Right

n = 42
print(str(n).zfill(5))
print(f"{n:05d}")

Convert to a string first, or use an f-string format spec.

Expecting the width to be the number of zeros

✗ Wrong

print("42".zfill(3))  # expecting 00042
Prints `042`: the width is the total length, not the number of zeros.

✓ Right

print("42".zfill(5))

Use the total length you want, here 5.

Expecting it to shorten a long string

✗ Wrong

print("123456".zfill(3))
Prints `123456` unchanged.

✓ Right

print("123456"[-3:].zfill(3))

Slice the string first if a hard limit is needed.

Python String zfill() Method: questions people ask

How do I add leading zeros to a number in Python?

Use str(n).zfill(width) or an f-string such as f"{n:05d}". Both give 00042 for 42 with width 5.

What is the difference between zfill() and rjust()?

zfill() always pads with zeros and keeps a leading sign in front, while rjust() can pad with any character and ignores signs.

Does zfill() work on integers?

No. It is a string method, so convert with str() first or use f"{n:05d}" on the integer.

Keep learning

Step-by-step lessons:
Python StringsPython String MethodsPython f-Strings

Related:
Python String center() MethodPython String format() MethodHow to Convert an Int to a String in PythonPython String isdigit() Method

More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() Method See all →