Python Reference Library →

Python String zfill() Method – Pad String with Zeros

Let’s look at string zfill() method – pad string with zeros

  1. Notice

    zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = '-42'
result = text.zfill(5)
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'-42'Object 2 · str'-0042'
This step

zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.

Picture string zfill() method – pad string with zeros

zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.

Image brief

Alt text
A diagram for Python String zfill() Method – Pad String with Zeros. Instructions: text = '-42' result = text.zfill(5) print(result). Displayed result: -0042. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: '-42'; object 2: '-0042'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String zfill() Method – Pad String with Zeros". Teaching focus: zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = '-42' result = text.zfill(5) print(result) | v DISPLAYED RESULT -0042 FINAL BINDINGS text --> object 1 (str) result --> object 2 (str) object 1: '-42' object 2: '-0042' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String zfill() Method – Pad String with Zeros. Instructions: text = '-42' result = text.zfill(5) print(result). Displayed result: -0042. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: '-42'; object 2: '-0042'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = '-42'
result = text.zfill(5)
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'-42'Object 2 · str'-0042'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
-0042

What do you expect this program to print?

What does this example display?

text = '-42'
result = text.zfill(5)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

-0042

Follow the printed values in order. The output is: -0042

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.

zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.

More examples and reference

Python String zfill() Method

The zfill() method in Python is used to pad a numeric string with leading zeros, so that the resulting string reaches a specified length. It's especially useful when formatting numbers like invoice IDs, codes, or timestamps.

Syntax

string.zfill(width)

Parameter:

  • width – The total length of the resulting string (including leading zeros).

Returns:

  • A copy of the string left-padded with zeros to make it at least width characters long.

Example 1: Simple Zero Padding

text = "42"
print(text.zfill(5))
00042

Example 2: zfill() with Negative Numbers

text = "-42"
print(text.zfill(5))
-0042

Note: The negative sign is preserved in its original position.

Example 3: No Padding if Already Long Enough

text = "12345"
print(text.zfill(4))
12345

The string remains unchanged because its length is already more than the specified width.

Use Cases

  • Formatting IDs with fixed length (e.g., invoice IDs like INV000123)
  • Preparing data for export to systems that require zero-padded numbers
  • Display time values like 05 instead of 5

Common Mistakes

  • Using zfill() on non-string types like integers will raise an error. Convert to string first.
  • Confusing zfill() with rjust() or ljust() which pad with spaces by default.

Interview Tip

zfill() is handy in problems requiring fixed-width formatting or normalization of numeric string data before comparison.

Summary

  • zfill() pads the string on the left with 0s until it reaches the specified length.
  • It preserves the sign position for negative numbers.
  • Only works on string types — use str() on numbers before calling zfill().

Practice Problem

Write a program that asks the user for a number and prints it as a 6-digit zero-padded string.

user_input = input("Enter a number: ")
print("Padded Number:", user_input.zfill(6))

What to remember

Connect what you noticed in the example with the idea below.

Remember

zfill() pads with zeros to a minimum width and places them after an initial sign. It returns text, not a number.

Watch out for this idea

zfill() inserts zeros before a leading minus sign.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →