bin函数

# 转为二进制字符串

# 整数
print(bin(-2))  # 负整数  '-0b10'
print(bin(+2))  # 正整数  '0b10'

# 非整数必须通过定义__index__()函数实现返回一个整数

更多:

  oct函数

  hex函数

  特殊方法

    __index__

    __int__

原文地址:https://www.cnblogs.com/ShuComputerProgram/p/10339591.html