How to Pad a Number with Leading Zeros in Python
f"{7:03d}" gives '007'. For text that is already a string, "7".zfill(3) gives the same '007'.Leading zeros are needed for invoice numbers (INV-00042), file names (image_007.png), times (09:05) and fixed-width codes. The catch is that an integer cannot hold leading zeros: 007 is not valid number syntax and int("007") is just 7. So padding always produces a string, which is exactly what you want for display.
There are three good ways to do it; which one you choose depends on whether you start with a number or a string.
| Number to padded string | f"{n:05d}" |
|---|---|
| String to padded string | s.zfill(5) |
| Result type | str, never int |
| Width meaning | Minimum total length; longer values are never cut |
Pad a number with zeros using an f-string
In an f-string, the format spec 05d means: pad with 0, make it at least 5 characters wide, treat the value as a decimal integer. The width can also come from a variable using nested braces.
order = 42
print(f"INV-{order:05d}")
width = 3
print(f"{7:0{width}d}")Pad a string with zfill()
str.zfill(width) pads a string on the left with zeros. It understands a leading sign, so the zeros go after a minus or plus sign. It only works on strings, so convert numbers with str() first.
print("42".zfill(5))
print("-42".zfill(5))
print(str(9).zfill(2))Pad with format() or rjust()
format(7, "03d") and "{:03d}".format(7) produce the same result as the f-string and are useful in older code. str.rjust(width, "0") also works for strings, but unlike zfill() it does not treat a minus sign specially, so "-5".rjust(4, "0") gives '00-5'.
Pad floats and use other fill characters
For floats, combine width and precision: f"{3.14159:08.2f}" gives '00003.14'. To pad with something other than zero, put the fill character before the alignment sign: f"{7:*>4}" gives '***7' (the > means right-align). Related: centering text with center().
Which zero-padding method should you use?
| Method | Code | Result for 7, width 3 |
|---|---|---|
| f-string | f"{n:03d}" | '007' |
| format() | format(n, "03d") | '007' |
| str.format() | "{:03d}".format(n) | '007' |
| zfill() | str(n).zfill(3) | '007' |
| rjust() | str(n).rjust(3, "0") | '007' |
| Float | f"{n:06.2f}" | '007.00' |
Examples you can run
Press Run, then edit the code and run it again.
Number with an f-string
05d pads to five digits with zeros.
for order in [7, 42, 1305]:
print(f"INV-{order:05d}")Expected outputINV-00007
INV-00042
INV-01305
String with zfill()
zfill() is perfect when the data is already text, such as an ID read from a file.
codes = ["5", "73", "2048"]
print([code.zfill(4) for code in codes])Expected output['0005', '0073', '2048']
Format a clock time
Two-digit hours and minutes use 02d.
hours, minutes = 9, 5
print(f"{hours:02d}:{minutes:02d}")Expected output09:05
Edge cases: negatives and values that are already wide
zfill() keeps the sign in front; the width never truncates.
print(f"{-42:05d}")
print("-42".zfill(5))
print(f"{123456:03d}")Expected output-0042
-0042
123456
Common mistakes
Trying to write a number with leading zeros
✗ Wrong
invoice = 007✓ Right
invoice = 7
print(f"{invoice:03d}")Store the number as an int and pad it only when you display it.
Calling zfill() on an int
✗ Wrong
print(7.zfill(3))✓ Right
print(str(7).zfill(3))zfill() is a string method. Convert with str() first.
Using d with a string value
✗ Wrong
print(f"{'7':03d}")✓ Right
print(f"{int('7'):03d}")Convert to int first, or use zfill() on the string.
How to Pad a Number with Leading Zeros in Python: questions people ask
How do I add leading zeros to a number in Python?
Use an f-string like f"{n:05d}" or str(n).zfill(5). Both return a string such as '00042'.
Why can't I store an integer with leading zeros?
Integers store only a value, and the value of 007 is just 7. Leading zeros are a display choice, so format the number into a string when you show it.
How do I remove leading zeros from a number string?
Use int(text) to get the number, or text.lstrip("0") to keep a string. Note lstrip turns "000" into an empty string.
Keep learning
Step-by-step lessons:
Python f-StringsPython StringsPython Numbers
Related:
Python String zfill() MethodPython String format() MethodHow to Convert an Int to a String in PythonPython String center() Method
More in working with text:
How to Reverse a String in PythonHow to Check if a String Contains a Substring in PythonHow to Remove Whitespace from a String in PythonHow to Split a String into Words in PythonHow to Compare Strings Ignoring Case in PythonHow to Capitalize the First Letter of a String in Python See all →