Advertisement
# Signature of zfill() method
zfill(length)
Parameters | Description |
---|---|
length | This is the length of an invoked string that we want to achieve by filling its beginning with zeroes. It is a non-optional attribute. |
# Python - Example of zfill() method
# Creating a string
s1 = ('United Kingdom')
# Printing the original string
print('Value of string : ', s1)
# Calling the zfill() method on string object
print('Calling the zfill() method on string : ', s1.zfill(20))
Value of string : United Kingdom
Calling the zfill() method on string : 000000United Kingdom
# Python -Example of zfill() method
# Creating a string
s1 = ('99.76899')
# Printing the original string
print('Original string before calling zfill() : ', s1)
# Calling the zfill() method on string object
# returns a string with zeroes at its beginning, until its length is 20
print('Calling the zfill() method : ', s1.zfill(20))
# Printing the original string
print('Original string after calling zfill() : ', s1)
Original string before calling zfill() : 99.76899
Calling the zfill() method : 00000000000099.76899
Original string after calling zfill() : 99.76899
Advertisement
# Python - zfill() method example.
# Creating a string
s = 'Smile as much as you can!'
# Printing the original string
print('Original string is : ', s)
# Result of calling zfill() is made to reference by the reference of original string.
s = s.zfill(30)
# Old reference variable pointing to the new string
print('String value after zfill() method : ', s)
Original string is : Smile as much as you can!
String value after zfill() method : 00000Smile as much as you can!
# Python - zfill() method example.
# Creating a string
s = 'Eat healthy, live healthy!'
# Printing the original string
print('Original string is : ', s)
# Finding the length of string by calling len() method
print('length of string : ', len(s))
# Calling the zfill() method on string object
# by passing its parameter length a value of 10
# which is already less than the length of the invoked string i.e. 26
# Hence, no filling will be made.
print(s.zfill(10))
# Calling the zfill() method on string object
# by passing its parameter length a value of 20
# which is already less than the length of the invoked string i.e 26
# Hence, no filling will be made.
print(s.zfill(20))
# Calling the zfill() method on string object
# by passing its parameter length a value of 20
# which is one length greater than the length of the invoked string i.e 26
# Hence, one zero will be added at the beginning of the string' content.
print(s.zfill(27))
Original string is : Eat healthy, live healthy!
length of string : 26
Eat healthy, live healthy!
Eat healthy, live healthy!
0Eat healthy, live healthy!
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement