Python String zfill() Method


Contents

Description

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

The Python zfill() method in Python string fills the string at left with 0 digit in order to make the string of length width. Thus it returns a string that contains a sign prefix + or - placed before the 0 digit. 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 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 above program is run, it produces the following resul:

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.



Navigation
Tutorial
Tutorial
Tutorial