How to use hex() without 0x in Python?

后端 未结 5 1798
失恋的感觉
失恋的感觉 2020-11-28 04:24

The hex() function in python, puts the leading characters 0x in front of the number. Is there anyway to tell it NOT to put them? So 0xfa230

5条回答
  •  暖寄归人
    2020-11-28 04:34

    Use this code:

    '{:x}'.format(int(line))
    

    it allows you to specify a number of digits too:

    '{:06x}'.format(123)
    # '00007b'
    

    For Python 2.6 use

    '{0:x}'.format(int(line))
    

    or

    '{0:06x}'.format(int(line))
    

提交回复
热议问题