Python Tutorial Python Advanced Python References Python Libraries

Python String - zfill() Method



The Python zfill() method returns the string with zero (0) added at the start of the string until its length reaches the specified length.

If the specified length is less than the length of the string, no filling is done.

Syntax

string.zfill(length)

Parameters

length Required. specify length of string after filling with zero.

Return Value

Returns the string with zero (0) added at the start of the given string until its length reaches the specified length.

Example:

In the example below, zfill() method returns the string with zero (0) added at the start of the string so that the length of the string reaches specified length. If the specified length is less than the length of the string, no filling is done.

MyString = "123456"
print(MyString.zfill(5))

MyString = "123456"
print(MyString.zfill(10))

The output of the above code will be:

123456
0000123456

❮ Python String Methods