Python String zfill() Method

Example

Zero-fill strings until the length is 10 characters:

txt = "50"
x = txt.zfill(10)
print(x)

Run Instance

Definition and Usage

The zfill() method adds zeros (0) at the beginning of the string until the specified length is reached.

If the value of the len parameter is less than the length of the string, no padding is performed.

Syntax

string.zfill(len)

Parameter Value

Parameter Description
len Required. Number, specifying the position of the element to be deleted.

More Examples

Example

Zero-fill strings until they are 10 characters long:

a = "hello"
b = "welcome to my world"
c = "10.000"
print(a.zfill(10))
print(b.zfill(10))
print(c.zfill(10))

Run Instance