Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.zfill

Pads a string with ASCII zeroes on the left to reach the requested width.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.zfill(width)

Pads a string with ASCII zeroes on the left to reach the requested width.

An initial sign stays before the padding. Text already at or above the width keeps its original length.

Pads with ASCII zeroes up to the minimum text width. It recognizes an initial plus or minus sign and inserts padding after that sign. Width five turns -42 into -0042; width two cannot shorten 123.

assert "-7".zfill(4) == "-007" and "−7".zfill(4) == "00−7"

Examples

print("-42".zfill(5))
print("123".zfill(2))

Result

-0042
123

Pads with ASCII zeroes up to the minimum text width. It recognizes an initial plus or minus sign and inserts padding after that sign. Width five turns -42 into -0042; width two cannot shorten 123.

Sources