Python String zfill() Method Tutorial

In this section, we will learn what String zfill() method is and how to use it in Python.

What is String zfill() Method in Python?

The Python string zfill() method stands for zero-fill and is used to fill the beginning of a string value with the number 0.

Basically, this method takes a number as its argument, and that defines a length. Now the method checks the target string value and if the current length of that string is less than the value specified as the argument of this method, then this method adds 0s to the begging of the string value until the length match the argument of the method.

Otherwise, if the length provided as the argument of the method is not greater than the current length of the target string value, then that value will return without any 0s added to the begging.

Python String zfill() Method Syntax:


String zfill() Method Parameter

The method takes one argument and that is a length for the new string that will return as a result of calling this method.

String zfill() Method Return Value

The return value of this method is a copy of the target string value (that this method is invoked with) with the specified length and 0s added to the begging part of it (if the specified length is greater than the current length of the string value, of course).

Example: using python string zfill() method

s1= "Think BIG"

result = s1.zfill(30)

result2 = s1.zfill(4)




000000000000000000000Think BIG

Think BIG

Top Technologies