zfill() makes a string at least width characters long by adding zeros on the left. "42".zfill(5) is "00042". It is the usual way to give numbers a fixed number of digits, such as 07 for a month or 0012 for a file number.
It is aware of signs: a leading + or - stays at the very front, and the zeros go after it. If the string is already as long as width, or longer, it is returned as it is. The method returns a new string.
text.zfill() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Pass the total width. It is required.
python
padded = text.zfill(width)
Call
What it does
"42".zfill(5)
Returns '00042'. Three zeros are added on the left.
"-42".zfill(5)
Returns '-0042'. The minus sign stays at the front.
"python".zfill(3)
Returns 'python'. The text is already longer than the width, so it is unchanged.
For other fill characters use rjust(). To pad on the right use ljust(). To convert text such as "007" back into a number, use int().
Library declarations
python
str.zfill(width) -> str
# returns: a new string at least width characters long
Parameters
Parameter
Description
width
Required. The length of the final string, counting any sign. If it is not larger than the length of the text, nothing is added.
Return Value
Return Value
Description
str
A new string of length max(width, len(text)), padded with zeros after any leading sign. The original string is not changed.
Python Examples
Example 1 — Give Numbers a Fixed Width
Pad short numbers with zeros, and see that a long number is not cut.
A minus sign stays first. The zeros are added after the -, so the result still looks like a negative number.
A plus sign too. A leading + is also kept at the front.
rjust() ignores signs. It pads before the minus sign and gives 000-42, which is not a valid number.
Any text works. The string "3.5" is padded like any other, giving 0003.5.
Example 3 — Format a Time and a File Number
Pad each part of a time to two digits, and a file number to four.
python
parts = [str(n).zfill(2) for n in (7, 5, 9)]
print(":".join(parts))
print("file_" + str(12).zfill(4) + ".txt")
Output
07:05:09
file_0012.txt
How It Works
Pad every part. The comprehension turns 7, 5, and 9 into the strings 07, 05, and 09.
Join with colons.":".join(parts) builds the clock text 07:05:09.
Four digits for a file number.str(12).zfill(4) gives 0012, so file names sort in the right order.
Frequently Asked Questions
It returns a copy of the string padded on the left with zeros to a given width. A leading plus or minus sign stays at the front.
Convert it to a string and use zfill, for example str(7).zfill(3) gives "007". You can also use an f-string such as f"{7:03d}".
zfill() always pads with zeros and keeps a leading sign at the front. rjust() can pad with any one character, but it treats a sign like an ordinary character.
zfill() is a string method. Integers do not have it, so convert the number first with str().
No. Strings are immutable, so zfill() returns a new string. Assign the result to keep it.