Python convert decimal to hex

What about this:

hex(dec).split('x')[-1]

Example:

>>> d = 30
>>> hex(d).split('x')[-1]
'1e'

By using -1 in the result of split(), this would work even if split returned a list of 1 element.

Leave a Comment