Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'{:08b}' .format(i) Equivalent in Python 2.x

I have a small bit of code in Python 3 -

'{:08b}' .format(i)

that gives an error in Python 2.x. Does anyone know the equivalent?

like image 289
brett Avatar asked Jan 12 '23 14:01

brett


1 Answers

Your original code actually works in Python 2.7. For Python 2.6, you need to introduce a reference to your format argument - either an index (0):

'{0:08b}'.format(i)

or a name:

'{x:08b}'.format(x=i)  # or:
'{i:08b}'.format(i=i)  # or even:
'{_:08b}'.format(_=i)  # (since you don't care about the name)

Strangely enough, this particular quirk doesn't seem to be mentioned in the documentation about string formatting :(

like image 122
Xion Avatar answered Jan 22 '23 16:01

Xion