Quick Clue – How to fill “zeros” in the left side using Python

Well, today I need fill a string putting “zeros” in the left side. After a quick search on the web, I found a cool function in Python called “zfill” that do this dirty work for me:

number='1'
print number.zfill(4)
0001

Of course, you can see that the “number” variable is a “string”. And the parameter “4” means the number of characters that will contain the final string.

 

Reference: http://docs.python.org/2/library/stdtypes.html

Leave a Reply

Your email address will not be published. Required fields are marked *