Python Function Reference

str.zfill

Pad the string with zeros on the left up to the length width.

Python 3.13
str.zfill(width)

Parameters

widthint
The minimum length of the result.

Return value

A new string; if the original is already at least width long, it is returned unchanged.

Examples

Padding with zeros on the left

Python 3.13
print('42'.zfill(5))
00042

Zeros after the minus sign

Python 3.13
print('-7'.zfill(4))
-007

Details

A leading + or - sign stays first, and the zeros go after it.

See also