zfill() function in python by R4R Team

- zfill() is a python in-build function which add zero at the starting of the string.

Syntax-

str.zfill(width)

- here str is the given string.
- and width is the final length of the string.

Example

string is s="mytext"
s.zfill(10) will give "0000mytext"

string is s="mytext"
s.zfill(8) will give "00mytext"


program-

s="mytext"
print(s.zfill(8))
print(s.zfill(10))
print(s.zfill(12))
print(s.zfill(15))
print(s.zfill(20))
print(s.zfill(24))
print(s.zfill(28))


output-

00mytext
0000mytext
000000mytext
000000000mytext
00000000000000mytext
000000000000000000mytext
0000000000000000000000mytext


-In this program, given string is "mytext" and we apply zfill() with different lengths like s.zfill(10) will add 4 more 0 at the starting of the string. and similarly other work.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!