Python - String zfill() Method


Advertisements

The string zfill() method is used to return 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.

Example:

In the below example, 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))

Output

123456

0000123456

String Methods
Advertisements