Python String | zfill()

zfill() method returns a copy of the string with ‘0’ characters padded to the leftside of the given string.

Syntax :

str.zfill(length)

Parameters :



length : length is the length of the returned string from zfill() with ‘0’ digits filled to the leftside.

Return :

Returns a copy of the string with '0' characters   
padded to the leftside of the given string.

CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

text = "geeks for geeks"
  
print(text.zfill(25))
  
print(text.zfill(20))
  
# Given length is less than
# the length od original string
print(text.zfill(10))

chevron_right


Output :

0000000000geeks for geeks
00000geeks for geeks
geeks for geeks

CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

number = "6041"
print(number.zfill(8))
  
number = "+6041"
print(number.zfill(8))
  
text = "--anything%(&%(%)*^"
print(text.zfill(20))

chevron_right


Output :

00006041
+0006041
-0-anything%(&%(%)*^


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.