Unicode Literals Invoking Invalid Syntax

The following code:

s = s.replace(u"&", u"&") 

causes an error in python:

 SyntaxError: invalid syntax 

removing u before " fixes the problem, but it should work just as it is? I'm using Python 3.1

+4
source share
3 answers

u no longer used in Python 3. String literals are unicode by default. See What's New in Python 3.0 .

You can no longer use u"..." literals for Unicode text. However, for binary data, you must use the literals b"..." .

+10
source

In Python 3, strings are unicode. There is no need (and, as you discovered, you cannot) put u in front of a string literal to denote unicode.

Instead, you need to put b before the byte literal to indicate that it is not Unicode.

+3
source

In Python3.3 +, the unicode literal is valid again, see What's New in Python 3.3 :

New syntax functions:

A new way out of the expression for delegating the generator.
The u'unicode syntax is again accepted for str objects.

+1
source

All Articles