SyntaxError: invalid character in identifier
Exception: SyntaxErrorCategory: parse timeRaised at: before any line runs
What this error means
A character in your source looks like ordinary punctuation but is not the ASCII character Python expects. Modern Python names it precisely, down to the code point:
File "demo.py", line 2
print(“Hello”)
^
SyntaxError: invalid character '“' (U+201C)
The U+201C is the useful part. Python is not complaining about your logic — it is telling you that the character at that position is a typographic left double quotation mark, not the plain " (U+0022) the language grammar accepts.
Where these characters come from
- Word processors. Word, Google Docs and Pages apply 'smart quotes' automatically, turning
"into“and”, and'into‘and’. - PDFs and screenshots of code, re-typed by hand or copied out of a viewer.
- Chat apps. WhatsApp, Slack and iMessage all substitute smart quotes and long dashes in normal prose, and the substitution survives the paste into your editor.
- Autocorrect replacing a hyphen with an en dash (
–, U+2013) or em dash (—, U+2014), or turning a straight apostrophe into a curly one inside a variable name. - Non-breaking spaces (U+00A0) from web pages, which look exactly like a space but are not one.
The pattern is always the same: the code was written in prose software first. The cure is to paste code through a plain-text buffer, or type it directly in your editor.
The usual suspects
Each of these has an ASCII twin that Python does accept:
“U+201C and”U+201D → use"‘U+2018 and’U+2019 → use'–U+2013 en dash and—U+2014 em dash → use-…U+2026 ellipsis → use...U+00A0 non-breaking space → use a normal space=fullwidth equals and other fullwidth forms → use=
print(“Hello”) # ❌ SyntaxError: invalid character '“' (U+201C)
print("Hello") # ✅
x = 5 – 2 # ❌ en dash
x = 5 - 2 # ✅
How to find the invisible ones
The dashes and quotes are visible once you know to look. Non-breaking spaces and zero-width characters are not — find them by code point:
with open("demo.py", encoding="utf-8") as f:
for lineno, line in enumerate(f, start=1):
for col, ch in enumerate(line, start=1):
# printable ASCII plus the newline is all a .py file needs
if ch != "\n" and not (" " <= ch <= "~"):
print(f"line {lineno} col {col}: {ch!r} U+{ord(ch):04X}")
In your editor, turn on 'Render Whitespace' (VS Code: *View → Render Whitespace*) and 'Show Unicode highlight' — VS Code marks suspicious invisible characters automatically. Most editors also have a Save with Encoding / Convert to ASCII action that will refuse or flag the offending bytes.
How to debug it
- Read the message — modern Python prints the exact character and its U+ code point. That is the whole diagnosis.
- Fix that one character, then re-run: pasted code usually carries several, and Python reports them one at a time.
- If the code came from a document or a chat, retype the affected line by hand rather than hunting character by character.
- Run the scan above to list every non-ASCII character in the file at once instead of fixing them one per run.
- Set your editor's default encoding to UTF-8 and turn off smart quotes in whatever app you copy from.
Fix it interactively
These free problems practice the string and syntax rules — repair them in the browser:
Practice more Python bugs →Related messages worth knowing
- SyntaxError: the parent guide — the parse-time family this belongs to.
- unterminated string literal — a smart quote can cause this too — it opens a string Python never closes.
- UnicodeDecodeError — the encoding cousin — wrong bytes when reading data, not source.
🇮🇳 Hindi में समझें
SyntaxError: invalid character in identifier ka matlab hai aapke code me koi aisa character hai jo dikhta normal punctuation jaisa hai, par Python grammar use accept nahi karta. Sabse common: Word, Google Docs, WhatsApp ya PDF se copy kiye gaye smart quotes — “ (U+201C) ki jagah ", ’ (U+2019) ki jagah ', aur en/em dash (–, —) ki jagah -. Python 3.10+ message me character ka code point bhi bata deta hai, jaise invalid character '“' (U+201C) — wahi diagnosis hai. Fix: us character ko ASCII se badal do. Non-breaking space jaise invisible characters ke liye file scan karo aur har character ka ord() dekho. Sabse aasan bachav: code ko editor me khud type karo, prose software se paste mat karo.