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?

1

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.

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.

Comments

Your Answer

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.