A Tip A Day — Python Tip #12: Using zfill() for ‘0’ padding | Dev Skrol

zfill() function is type of padding function.

This function fills only with ‘0’. Takes the resultant width of the string as argument.

This is useful to pad a numeric string values.

string = "10" 
string.zfill(10)

Output:

'0000000010'

A Realtime scenario of usage:

In some of the applications we may have a 5 digit numeric string to represent any ID.

In that case, we can pad with zeros for the desired length of the string and do the increment by 1 (convert to int and increase 1).

ID = "15" 
ID = ID.zfill(5)
print("Original ID:" + ID)
next_ID = str(int(ID)+1).zfill(5)
print("Next ID:" + next_ID)

Output:

Original ID:00015 
Next ID:00016

I hope you enjoyed learning various types of split functions in python.

Lets explore more about Python in future tips.

More interesting Python tips:

A Tip A Day — Python Tip #9: 3 Interesting things about format {}

A Tip A Day — Python Tip #11: Split string using partition, rpartition & splitlines

A Tip A Day — Python Tip #10: 2 reasons to use Python Type Hints

All Other Tips

Other interesting concepts:

Artificial Intelligence — A brief Introduction and history of Deep Learning

Deep Learning — Classification Example

SpaCy Vs NLTK — Basic NLP Operations code and result comparison

Best way to Impute NAN within Groups — Mean & Mode

Originally published at https://devskrol.com on October 17, 2021.

--

--

Data Science & Machine Learning Enthusiast | Software Developer | Blogger | https://devskrol.com/ | www.linkedin.com/in/asha-ponraj-a5a76b50

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store