Search code examples
pythonpython-3.xunicodepython-2.xsix

What is a python 2/3 compatible alternative to the unicode() function?


Is there an alternative to the python 2 unicode() function that can be used in code that will work with both python 2 and 3, and will definitely produce unicode output in python 2?

  • the unicode() function does not exist in python 3
  • six.u(u'xyz') raises an error in python 2

I am writing test code where I definitely want to produce unicode output so it will blow up in the tests if it is combined with a non-unicode string somewhere in the execution path - eg

'stuff %s' % u'unistuff'

For the general case I've seen it suggested to just use str(), but in python 2 that does NOT produce unicode.

I guess I could do:

if six.PY3:
    unicode = str

but surely there is some officially supported way.

(And it's quite hard to google for this as all the results are about unicode strings rather than the unicode function itself).


Solution

  • Per the documentation (which I'd recommend reading...) you want six.text_type:

    Type for representing (Unicode) textual data. This is unicode() in Python 2 and str in Python 3.