Python Language

Unicode

Encoding and decoding

Always encode from unicode to bytes. In this direction, you get to choose the encoding.

>>> u'🐍'.encode('utf-8')
'\xf0\x9f\x90\x8d'

The other way is to decode from bytes to unicode. In this direction, you have to know what the encoding is.

>>> b'\xf0\x9f\x90\x8d'.decode('utf-8')
u'\U0001f40d'

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow