Richard
Richard

Reputation: 15882

convert integer to formated binary string in python2.6

In python2.7 "{:010b}".format(25) would return '0000011001' giving a 10 bit binary string representation of the number 25. In python2.6 the same command returns ValueError: zero length field name in format. Is there a different way doing simple formated conversions for python2.6

Upvotes: 1

Views: 246

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123480

Use the format() function, it's easier (no need for the template placeholder parts, only the formatter string is needed):

format(25, '010b')

but you ran into a simplification in Python 2.7, where you don't have to specify the positional parameter. The 2.6 equivalent is:

"{0:010b}".format(25)

Upvotes: 1

Related Questions