str.zfill() Pad with zeros
Last updated: 2026-09-22
str.zfill() Pad with zeros
str.zfill(width) pads on the left with zeros to total width; preserves sign.
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.zfill(width)
⚙️ Parameters
| width Required | Total width of the result string. |
|---|
Returns:str. The string left-padded with zeros to width; a leading sign is preserved.
Mutates the original:No (returns a new object)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
00042
-0042
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does zfill() do?
APads the left with '0' up to the specified width. '42'.zfill(5) gives '00042'.
QHow are negative numbers zero-padded?
AThe minus sign stays at the very front: '-42'.zfill(5) gives '-0042', without squeezing the sign between the digits.
QWhat happens when width is smaller than the original length?
AThe original string is returned. '123456'.zfill(3) returns '123456'.