String Methods
zfill()
Pad the left side with zeroes until text reaches a chosen width.
str.zfill(width)
str.zfill() pads the string on the left with ASCII '0' digits until it reaches the specified width.
Leading signs (+ or -) are handled properly by inserting zeros after the sign.
Parameters
| Name | Description |
|---|---|
| width | The total width of the returned string. |
Returns
A zero-padded string; a leading plus or minus sign stays at the front.
Try it
Run it and change it
number = "-42" print(number.zfill(5))
Worth knowing
- Numeric string formatting:
'-42'.zfill(5)returns'-0042', preserving the negative sign position.