How to make unicode string with python3

cnd picture cnd · Jul 25, 2011 · Viewed 198.7k times · Source

I used this :

u = unicode(text, 'utf-8')

But getting error with Python 3 (or... maybe I just forgot to include something) :

NameError: global name 'unicode' is not defined

Thank you.

Answer

John La Rooy picture John La Rooy · Jul 25, 2011

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.