Python zfill() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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)
CallWhat 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

ParameterDescription
widthRequired. 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 ValueDescription
strA 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.

python
print("7".zfill(3))
print("42".zfill(5))
print("12345".zfill(3))
print(str(8).zfill(2))

How It Works

  1. Fill up to the width. "7".zfill(3) adds two zeros to reach three characters.
  2. Any width. A width of 5 gives 00042.
  3. Long text is left alone. "12345" is already longer than 3, so nothing changes.
  4. Convert numbers first. str(8) makes the text "8", and zfill(2) turns it into 08.

Example 2 — How zfill() Treats Signs

Pad numbers with signs and a decimal point, and compare with rjust().

python
print("-42".zfill(6))
print("+42".zfill(6))
print("-42".rjust(6, "0"))
print("3.5".zfill(6))

How It Works

  1. A minus sign stays first. The zeros are added after the -, so the result still looks like a negative number.
  2. A plus sign too. A leading + is also kept at the front.
  3. rjust() ignores signs. It pads before the minus sign and gives 000-42, which is not a valid number.
  4. 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")

How It Works

  1. Pad every part. The comprehension turns 7, 5, and 9 into the strings 07, 05, and 09.
  2. Join with colons. ":".join(parts) builds the clock text 07:05:09.
  3. 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.

Back to the method index

See all 47 string method guides grouped by what they do.

Method Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

14 people found this page helpful