2014年3月22日 星期六

[Python] Formatted Binary

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

沒有留言:

張貼留言