integer to binary 0-padded

John S jstrickler at gmail.com
Fri Jun 17 07:57:13 EDT 2011


On Jun 15, 9:33 am, Olivier LEMAIRE <m.olivier.lema... at gmail.com>
wrote:
> You're right, I use Python 2.6.6

This works great in 2.6.5 and later (and probably earlier). You just
have to number your placeholders. The first set of braces formats i
(your value), the second set specifies the field with (i.e., 8):

>>> for i in xrange(10):
...     print "{0:0{1}b}".format(i,8)
...
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001



More information about the Python-list mailing list