Python int to binary string?

后端 未结 30 2037
执念已碎
执念已碎 2020-11-22 05:34

Are there any canned Python methods to convert an Integer (or Long) into a binary string in Python?

There are a myriad of dec2bin() functions out on Google... But I

30条回答
  •  野性不改
    2020-11-22 06:04

    Python's string format method can take a format spec.

    >>> "{0:b}".format(37)
    '100101'
    

    Format spec docs for Python 2

    Format spec docs for Python 3

提交回复
热议问题