← Back

Home › Errors › SyntaxError

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

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:

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

  1. Read the message — modern Python prints the exact character and its U+ code point. That is the whole diagnosis.
  2. Fix that one character, then re-run: pasted code usually carries several, and Python reports them one at a time.
  3. If the code came from a document or a chat, retype the affected line by hand rather than hunting character by character.
  4. Run the scan above to list every non-ASCII character in the file at once instead of fixing them one per run.
  5. 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

🇮🇳 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.

SyntaxError Hindi में पढ़ें →