>>> "hello".encode("hex")
'68656c6c6f'
相应的还可以
>>> '68656c6c6f'.decode("hex")
'hello'
查了一下手册,还有这些codec可用
Codec
Aliases
Operand type
Purpose
base64_codecbase64, base-64byte stringConvert operand to MIME base64bz2_codecbz2byte stringCompress the operand using bz2hex_codechexbyte stringConvert operand to hexadecimal representation, with two digits per byteidnaUnicode stringImplements RFC 3490. New in version 2.3. See also encodings.idnambcsdbcsUnicode stringWindows only: Encode operand according to the ANSI codepage (CP_ACP)palmosUnicode stringEncoding of PalmOS 3.5punycodeUnicode stringImplements RFC 3492. New in version 2.3.quopri_codecquopri, quoted-printable, quotedprintablebyte stringConvert operand to MIME quoted printableraw_unicode_escapeUnicode stringProduce a string that is suitable as raw Unicode literal in python source coderot_13rot13Unicode stringReturns the Caesar-cypher encryption of the operandstring_escapebyte stringProduce a string that is suitable as string literal in python source codeundefinedanyRaise an exception for all conversions. Can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired.unicode_escapeUnicode stringProduce a string that is suitable as Unicode literal in python source codeunicode_internalUnicode stringReturn the internal representation of the operanduu_codecuubyte stringConvert the operand using uuencodezlib_codeczip, zlibbyte stringCompress the operand using gzip