Open In App
Related Articles

Python String zfill()

Improve Article
Improve
Save Article
Save
Like Article
Like

Python String zfill() method returns a copy of the string with ‘0’ characters padded to the left side of the given string.

Syntax of zfill() methods

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 left side of the given string.

Example 1: How to use zfill() function in Python.

Python3




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))


Output: 

0000000000geeks for geeks
00000geeks for geeks
geeks for geeks

Example 2: zfill() methods with Sign Prefix

Python3




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


Output: 

00006041
+0006041
-0-anything%(&%(%)*^
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Mar, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials