Search code examples
pythonpython-3.xunicodeunicode-literals

Unicode literals causing invalid syntax


The following code:

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

is causing an error in python:

SyntaxError: invalid syntax

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


Solution

  • The u is 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, you must use b"..." literals for binary data.