zfill()

zfill(): Fill the string with zeros
Fill zeros up to the input length of the string
my_str='plus2net'
output=my_str.zfill(15)
print(output)

my_str='25'
output=my_str.zfill(5)
print(output)
output is here
0000000plus2net
00025

Using Integer

If we are using any integer then we have to convert the same to string by using str() before using. Here is the error message

AttributeError: 'int' object has no attribute 'zfill'
id=str(45) #convert to string
print(id.zfill(5)) # 00045
It works only with strings, so integers must be converted to strings before using zfill(). This is helpful when handling numerical IDs or fixed-length formats.
my_str = '25'
output = my_str.zfill(5)
print(output)  # Output: '00025'

Practical use case of zfill()

Employee numbers need to be formatted as five-digit, auto-incremented values, starting with leading zeros for smaller numbers. Using zfill(5) ensures this formatting.

For example, early employee IDs like 5, 78, and 147 will appear as 00005, 00078, and 00147, while later ones like 1587 and 11258 will naturally appear as 01587 and 11258. This guarantees a consistent 5-digit format across all employee numbers.

The zfill() method makes it simple to enforce this padding for uniform ID lengths.
All String methods


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer