How to convert an integer to hexadecimal without the extra '0x' leading and 'L' trailing characters in Python?

后端 未结 6 1119
栀梦
栀梦 2021-02-18 14:23

I am trying to convert big integer number to hexadecimal, but in result I get extra \"0x\" in the beginning and \"L\" at the and. Is there any way to remove them. Thanks. The nu

6条回答
  •  孤独总比滥情好
    2021-02-18 14:45

    Be careful when using the accepted answer as lstrip('0x') will also remove any leading zeros, which may not be what you want, see below:

    >>> account = '0x000067'
    >>> account.lstrip('0x')
    '67'
    >>>
    

    If you are sure that the '0x' prefix will always be there, it can be removed simply as follows:

    >>> hex(42)
    '0x2a'
    >>> hex(42)[2:]
    '2a'
    >>>
    

    [2:] will get every character in the string except for the first two.

提交回复
热议问题