logo

Python String Zfill() Method


Show

Description

This zfill() method in Python strings pads the string on the left where zeros are to fill the width.M

The Python zfill() method in Python string fills the string at left with 0 digits in order to make the string of length width. Thus it returns a string that contains a sign prefix + or - placed before the 0 digits. It may also return the original length of the string in case the width is less than that of a string length.

Syntax

Here is the syntax for zfill() method:

str.zfill(width)

Parameters

  • width - This is the final width of the string which one would get after filling zeros.

Return Value

This method in Python String returns a padded string.

Example

The following example below shows the usage of zfill() method.

#!/usr/bin/python

str = "this is string example....wow!!!";
print str.zfill(40)
print str.zfill(50)

When the above program is run, it produces the following result:

00000000this is string example....wow!!!
000000000000000000this is string example....wow!!!

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.