>>> '{0:08b}'.format(6)
00000110
Just to explain the parts of the formatting string:
{}
places a variable into a string0
takes the variable at argument position 0:
adds formatting options for this variable (otherwise it would represent decimal6
)08
formats the number to eight digits zero-padded on the leftb
converts the number to its binary representation
ref. http://stackoverflow.com/questions/10411085/converting-integer-to-binary-in-python
沒有留言:
張貼留言