Content · Programming · Python

Python str.zfill Method: Usage and Examples

The str.zfill(width) method pads a numeric-looking string with zeros on the left until it reaches the requested minimum width. An existing leading plus or minus sign stays at the front. See two practical examples, edge.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Order identifiers must display as fixed-width numeric strings without changing the underlying identifier value.

What You Need to Understand


The str.zfill(width) method pads a numeric-looking string with zeros on the left until it reaches the requested minimum width. An existing leading plus or minus sign stays at the front.

When to Use It


Order identifiers must display as fixed-width numeric strings without changing the underlying identifier value.

Step-by-Step Python Example


order_number = "57"
print(order_number.zfill(5))
print("-42".zfill(6))
print("102345".zfill(4))

How the Example Works


The output is 00057, -00042, and 102345. Strings already wide enough are left unchanged.

Example Output


00057
-00042
102345
00000812

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

invoice_id = 812
print(str(invoice_id).zfill(8))

Important Behavior and Edge Cases


Zero padding is a display convention, not a numeric conversion. "57" and "00057" represent different strings even though both can be converted to the same integer.

Try It Yourself


Display IDs 1, 20, and 3500 using a width of six characters.

Common Beginner Mistakes


  • Zero padding is a display convention, not a numeric conversion. "57" and "00057" represent different strings even though both can be converted to the same integer.
  • Applying a string method before defining whether text is intended for display, comparison, parsing, or input validation.

Key Takeaways


  • The str.zfill(width) method pads a numeric-looking string with zeros on the left until it reaches the requested minimum width.
  • The output is 00057, -00042, and 102345.
  • Zero padding is a display convention, not a numeric conversion.
Share this resourceSend this page to someone who may find it useful.