Python中的整数到十六进制转换


问题内容
a = 1
print hex(a)

上面给了我输出: 0x1

我如何获得输出0x01呢?


问题答案:

您可以使用format

>>> a = 1
>>> '{0:02x}'.format(a)
'01'
>>> '0x{0:02x}'.format(a)
'0x01'