PythonMastery
reference 3 min read · lesson 69 of 80 in Errors

SyntaxError: invalid character '“' (U+201C)

1 · The lesson

read

What this error means

Your file contains a character that looks like normal code but is a different character. Python code only accepts the plain straight quotes " and ', the plain minus sign - and the normal space. Word processors, PDFs, websites and chat apps often swap these for "nicer" versions: curly quotes “ ” ‘ ’, a long dash –, or a space that can't break across lines. They look almost the same to you, but Python reads them as unknown symbols and stops before running anything. The U+ code in the message is the exact name of the character, so you can tell which one it is.

When you see it

python
  File "main.py", line 1
    name = “Ada”
           ^
SyntaxError: invalid character '“' (U+201C)

A long dash where a minus sign should be:

python
  File "main.py", line 1
    total = 10 – 3
               ^
SyntaxError: invalid character '–' (U+2013)

A hidden non-breaking space gets its own wording, because you can't see it at all. The line looks normal, and the ^ points at an empty-looking spot:

python
  File "main.py", line 3
    print(x, "big")
            ^
SyntaxError: invalid non-printable character U+00A0

Why it happens

You copied code from somewhere that changes characters while you type or paste. The usual characters are:

You seeCodeWhat it isWhat Python wants
“ ”U+201C, U+201DCurly double quotes"
‘ ’U+2018, U+2019Curly single quotes'
–U+2013En dash-
—U+2014Em dash-
(nothing visible)U+00A0Non-breaking spacea normal space
(nothing visible)U+200BZero-width spacedelete it

Where they come from:

1. Word, Google Docs, Pages or Notes. These apps turn straight quotes into curly quotes as you type ("smart quotes"), and turn -- into a long dash.

2. PDFs and slides. Course notes and e-books are often typeset with curly quotes. Copying from a PDF also sometimes brings non-breaking spaces with it.

3. Websites, blogs and chat apps. WhatsApp, Slack, Teams, email and many blog themes change quotes, or add non-breaking spaces to keep text together on a line.

4. A phone keyboard. iPhone and Android keyboards type curly quotes by default.

How to fix it

Retype the character. Go to the line and column the ^ points at, delete the character, and type it again on your computer keyboard in your code editor. For quotes, retype both the opening and the closing quote.

Replace them all at once if you pasted a lot of code. Python can clean a file for you:

python
fixes = {
    "\u201c": '"', "\u201d": '"',   # curly double quotes
    "\u2018": "'", "\u2019": "'",   # curly single quotes
    "\u2013": "-", "\u2014": "-",   # en dash, em dash
    "\u00a0": " ",                  # non-breaking space
    "\u200b": "",                   # zero-width space
}

pasted = "name = \u201cAda\u201d\ntotal = 10 \u2013 3"
for bad, good in fixes.items():
    pasted = pasted.replace(bad, good)
print(pasted)
# name = "Ada"
# total = 10 - 3

To clean a real file, read it with open("main.py", encoding="utf-8").read(), run the same loop, and write it back.

Find the hidden ones. If the ^ points at what looks like an ordinary space, print every character on that line that isn't plain ASCII:

python
line = 'print(x,\u00a0"big")'
for position, char in enumerate(line):
    if ord(char) > 127:
        print(position, repr(char), f"U+{ord(char):04X}")
# 8 '\xa0' U+00A0

Stop it happening again. Write code in a code editor (VS Code, PyCharm, IDLE, or the editor on this site), never in a word processor. On a Mac, turn off "Use smart quotes and dashes" in System Settings, Keyboard, Text Input. When you share code in chat, put it in a code block (three backticks in Slack, Teams and Discord) so the app leaves the characters alone.

Curly quotes inside a string are fine. Python only objects when the character is part of the code itself. Inside a normal string, any character is allowed:

python
quote = "She said “hello” – twice."
print(quote)     # She said “hello” – twice.

When you'd see this in real code

  • Code pasted from a tutorial PDF or a slide deck for a class.
  • A snippet a colleague sent in Teams or WhatsApp without a code block.
  • A shell command copied from a blog, like pip install requests –upgrade with a long dash. Here the error comes from pip, not Python, but the cause is the same.
  • An answer copied from a chatbot into a notes app first, and from there into the editor.

See Also

Found this useful? Share it with a friend who’s learning Python.