4

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?

Zain Khan
3,8233 gold badges37 silver badges54 bronze badges
asked Dec 30, 2011 at 13:33
1
  • 4
    You should use the official Python 3.2 tutorial, since you are using Python 3.2: docs.python.org/3.2/tutorial. Commented Dec 30, 2011 at 13:41

2 Answers 2

7

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.

answered Dec 30, 2011 at 13:37
Sign up to request clarification or add additional context in comments.

Comments

3

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.

answered Mar 19, 2015 at 15:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.