Chapters
Python114 chapters

ReferenceString methods

str.zfill()

Pads with leading zeros to the given width.

Signature

text.zfill(width)

Parameters

NameMeans
widththe total length wanted

Returns

A new string, unchanged if it is already long enough.

Example

Python
print("7".zfill(3))
print("1234".zfill(3))
print("-7".zfill(4))

Output

007
1234
-007

See also