For a cross-version compatible solution, use binascii.hexlify:
>>> import binascii
>>> x = '\x01\x41\x42\x43'
>>> print x
ABC
>>> repr(x)
"'\\x01ABC'"
>>> print binascii.hexlify(x)
01414243
Since it .encode('hex')is misused encodeand removed in Python 3:
Python 3.3.1
Type "help", "copyright", "credits" or "license" for more information.
>>> '\x01\x41\x42\x43'.encode('hex')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
LookupError: unknown encoding: hex
source
share