Home / Python / Reference / String methods

Python string zfill() method

Pad a number-string with zeros on the left to a given width.

Syntax

str.zfill(width)

Parameters

widththe total length wanted

Return value

str

Example

Code

print("7".zfill(3), "42".zfill(5))
print(f"{7:03d}")          # the f-string way

Output

007 00042
007

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.