Official Python tutorial states that Unicode strings in Python can be used like this:
u'Hello World !'
But when I put it to IDLE - Python GUI of Python 3.2, it gives me a syntax error. Also Russian and Chinese text can be succcessfully stored in that Python strings, so I guess they are already Unicode.
Could you please explain what's happening?
-
4You should use the official Python 3.2 tutorial, since you are using Python 3.2: docs.python.org/3.2/tutorial.Eric O. Lebigot– Eric O. Lebigot2011年12月30日 13:41:07 +00:00Commented Dec 30, 2011 at 13:41
2 Answers 2
by default python 3.2 works with unicode strings so the u is no longer needed.
If you want to encode and decode strings you should use:
encoded = "unicodestring".encode("UTF8")
decoded = s.decode("UTF8")
The Python documetation states that:
Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. All text is Unicode; however encoded Unicode is represented as binary data. The type used to hold text is str
and
You can no longer use u"..." literals for Unicode text. However, you must use b"..." literals for binary data.
Comments
In Python3.3+ unicode literal is valid again, see What’s New In Python 3.3:
New syntax features:
New yield from expression for generator delegation.
The u'unicode' syntax is accepted again for str objects.