SyntaxError: invalid character in identifier (Fix)
SyntaxError: invalid character in identifier means Python found a character in your code that does not belong there.
This usually happens when code contains a character that looks normal but is not the normal Python version. Common examples include curly quotes, an em dash, or a hidden Unicode symbol copied from a website, PDF, or document.
This page shows what the error means, what causes it, how to find the bad character, and how to fix it.
Quick fix #
name = "Alice"
print(name)
# Common problem:
# print(“Hello”) # curly quotes cause the error
# Fix:
print("Hello")
In many cases, the fastest fix is:
- Delete the suspicious character
- Type it again manually
- Use normal keyboard characters only
What this error means #
Python found a character inside a name or line of code that it cannot use there.
A Python identifier is a name such as:
- A variable name
- A function name
- A class name
For example, these are identifiers:
user_name = "Alice"
def greet():
pass
class Person:
pass
The problem is often not the whole line. It is usually just one bad character on that line.
Also, the character may look correct on screen but still be different from the normal keyboard version.
What usually causes it #
Common causes include:
- Copying code from a web page, PDF, Word document, or chat app
- Using curly quotes like
“ ”or‘ ’instead of straight quotes - Using a dash like
–or—instead of the normal minus sign- - Pasting invisible characters or non-breaking spaces into code
- Using punctuation from another keyboard layout inside names
Common causes at a glance #
- Curly quotes copied from a website or document
- Em dash or en dash used instead of a minus sign
- Invisible Unicode character pasted into code
- Non-breaking space inside a line
- Invalid symbol inside a variable or function name
If you are new to Python syntax, it also helps to review Python syntax basics.
Example that causes the error #
Example 1: Curly quotes #
This line uses curly quotes, not normal double quotes:
print(“Hello”)
Python will raise a syntax error because “ and ” are not valid string delimiters in Python.
Correct version:
print("Hello")
Expected output:
Hello
Example 2: Em dash instead of minus sign #
This line uses an em dash:
result = 10 — 5
print(result)
That dash is not the normal minus sign, so Python cannot parse it correctly.
Correct version:
result = 10 - 5
print(result)
Expected output:
5
Example 3: Bad character in a name #
Here, the variable name contains an invalid symbol:
user—name = "Sam"
print(user—name)
The em dash is not valid in a Python identifier.
Correct version:
user_name = "Sam"
print(user_name)
Expected output:
Sam
How to fix it #
Try these fixes:
- Delete the suspicious character and type it again manually
- Replace curly quotes with normal quotes:
"or' - Replace long dashes with the normal minus sign:
- - Retype the whole line if you cannot see which character is wrong
- Paste the code into a plain text editor to remove rich-text formatting
- Check variable and function names for unusual symbols
If the error is inside a string example, reviewing Python strings explained can help you spot quote problems faster.
How to find the bad character #
Use the traceback to narrow it down.
1. Look at the line number #
Run your script:
python your_script.py
Python usually shows the exact line where the error happened.
2. Check the caret ^ #
Sometimes Python marks the position with a caret:
print(“Hello”)
^
SyntaxError: invalid character in identifier
The marker may point at or near the problem character.
3. Check suspicious characters closely #
Look carefully for:
- Quotes
- Dashes
- Spaces
- Punctuation
- Characters copied from another source
4. Retype the suspicious part #
Move through the line character by character and retype the part that looks wrong.
If needed, retype the whole line manually.
5. Compile the file to check syntax #
You can also ask Python to check the file without running it:
python -m py_compile your_script.py
This is useful when you want to find syntax problems quickly.
6. Try a plain text editor #
If the line still looks fine, paste it into a plain text editor and compare it with your code editor version.
7. UTF-8 mode may help in some environments #
python -X utf8 your_script.py
This does not fix invalid characters by itself, but it can help avoid encoding-related confusion in some setups.
Valid vs invalid examples #
Python identifiers should be simple and predictable.
Valid names #
These are valid identifiers:
name = "Ana"
user_1 = "Tom"
total_count = 3
Why they work:
- They use letters
- They use numbers only after the first character
- They use underscores
Invalid names #
These are not valid:
user name = "Tom"
user—name = "Tom"
2name = "Tom"
Problems:
user namecontains a spaceuser—namecontains an em dash2namestarts with a number
Valid string quotes #
Use normal quotes only:
message = "Hello"
text = 'Python'
Invalid string quotes #
These will cause problems:
message = “Hello”
text = ‘Python’
Valid operators #
Use standard Python operators from your keyboard:
answer = 8 - 3
Not fancy punctuation copied from formatted text.
If you see a more general syntax problem, you may also need to fix SyntaxError: invalid syntax.
Prevention tips #
To avoid this error in the future:
- Write code in a code editor instead of a word processor
- Avoid copying code from formatted documents when possible
- If you paste code, test it right away
- Use a Python-friendly editor that highlights syntax errors clearly
- Keep your keyboard layout consistent when writing code
A good habit is to keep identifiers simple:
- Use letters
- Use numbers after the first character
- Use underscores
- Avoid fancy punctuation
Related errors to check #
Sometimes this error appears alongside other beginner syntax issues.
Related pages:
- SyntaxError: invalid syntax
- SyntaxError: EOL while scanning string literal
- NameError: name is not defined
FAQ #
What is an identifier in Python? #
An identifier is a name you create in Python, such as a variable name, function name, or class name.
Can Unicode characters be used in Python identifiers? #
Some Unicode letters can be valid in identifiers, but many copied symbols and punctuation marks are not. If you are a beginner, it is safest to use letters, numbers, and underscores only.
Why does the line look correct but still fail? #
The bad character may look almost the same as a normal one. This often happens with curly quotes, long dashes, or invisible spaces.
What is the fastest fix? #
Delete the part of the line that looks suspicious and type it again manually in your code editor.