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"
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)
I hope you enjoyed learning various types of split functions in python.
Lets explore more about Python in future tips.