zfill() Function in Python

zfill() Function in Python pads string on the left with zeros to the required width.

Syntax of zfill() Function in Python

str.zfill(width)

width – width of the string.  The Final width which we would get after filling zeros.

 

Example of zfill() Function in Python

#zfill() for positive number string
number1="345"
print number1.zfill(2)
print number1.zfill(4)

# zfill() for negative number string
number2="-675"
print number2.zfill(6)

#zfill() for Text String
text="This is sample text"
text.zfill(40)

so the output will be

‘345’
‘0345’
‘-00675’
‘000000000000000000000This is sample text’

 

prev zfill() Function in Python                                                                                                                next zfill() Function in Python