Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 457 Bytes

python.md

File metadata and controls

27 lines (21 loc) · 457 Bytes

Python cheat sheet

Pad a number with zeros on the left

From StackOverflow

Strings:

>>> n = '4'
>>> print n.zfill(3)
>>> '004'

And for numbers:

>>> n = 4
>>> print '%03d' % n
>>> 004
>>> print format(4, '03') # python >= 2.6
>>> 004
>>> print '{0:03d}'.format(4)  # python >= 2.6
>>> 004
>>> print('{0:03d}'.format(4))  # python 3
>>> 004