Python Glossary Русский

all Python 3Strings (str)function

str.zfill

alignment

Pad the string on the left with zeros to the given width; a leading sign is kept.

Syntax

s.zfill(width)

That sign handling is exactly what separates zfill from rjust with '0': zfill steps over a leading + or - and pads after it, so the sign never ends up buried in zeros. Only a sign in the very first position counts, and a string already at least width long is returned unchanged — nothing is ever truncated.

Examples

print('42'.zfill(5))    # → 00042
print('-42'.zfill(5))   # → -0042
print('3.14'.zfill(6))  # → 003.14

See also

Open in the full glossarydocs.python.orgReport an inaccuracy