Integer to Hexadecimal Conversion in Python

后端 未结 6 1716
执念已碎
执念已碎 2021-01-13 11:37
a = 1
print hex(a)

The above gives me the output: 0x1

How do I get the output as 0x01 instead?

6条回答
  •  再見小時候
    2021-01-13 12:12

    You can use format :

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

提交回复
热议问题