How to make unicode string with python3 How to make unicode string with python3 python-3.x python-3.x

How to make unicode string with python3


Literal strings are unicode by default in Python3.

Assuming that text is a bytes object, just use text.decode('utf-8')

unicode of Python2 is equivalent to str in Python3, so you can also write:

str(text, 'utf-8')

if you prefer.


What's new in Python 3.0 says:

All text is Unicode; however encoded Unicode is represented as binary data

If you want to ensure you are outputting utf-8, here's an example from this page on unicode in 3.0:

b'\x80abc'.decode("utf-8", "strict")


As a workaround, I've been using this:

# Fix Python 2.x.try:    UNICODE_EXISTS = bool(type(unicode))except NameError:    unicode = lambda s: str(s)