Home > Article > Backend Development > The wonderful uses of python encode and decode
String decoding:
There is a string:
'\u4fee\u6539\u8282\u70b9\u72b6\u6001\u6210\u529f'
Want to convert to:
u"u4feeu6539u8282u70b9u72b6u 6001u6210u529f"
can be passed as follows
>> a = '\u4fee\u6539\u8282\u70b9\u72b6\u6001\u6210\u529f'
>>> a.decode('unicode_escape')
u'u4feeu6539u8282u70b9u72b6u6001u621 0u529f'
string converted to 16 Base:
>>> "hello".encode("hex")
'68656c6c6f'
The corresponding one is OK
>>> '68656c6c6f'.decode("hex")
'hello'
You can check the manual to see which codecs are available