How to fix "SyntaxError: invalid character" in Python
Reza Lavarian
Posted on February 1, 2023
✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza
The error “SyntaxError: invalid character” in Python happens once Python encounters an invalid character (according to Python’s syntax) in your statements.
You may have invalid characters in your code if:
- You’ve copied a code snippet from a web page, a PDF document, or another formatted text containing invalid characters.
- Your active keyboard isn’t English, and you’ve typed an invalid character.
Here’s what the error looks like:
File /dwd/sandbox/test.py, line 1
book_title = ´Head First Python
^
SyntaxError: invalid character '´' (U+00B4)
This error also indicates which character causes the syntax error.
Retyping the indicated invalid character fixes the issue instantly.
How to fix the "SyntaxError: invalid character" error in Python
Any invalid character can lead to the "SyntaxError: invalid character" error.
To fix it:
- First: Inspect the error and find the invalid character.
- Then: Retype it with your English keyboard.
Below are the most common characters that cause the issue:
- A pair of invalid quotation marks like
´´
- An invalid comma
","
instead of","
- Using
"—"
instead of"-"
when subtracting numbers
Let's explore each scenario over some examples.
A pair of invalid quotation marks like ´´
: All string literals must be quoted in Python, with single, double, or triple quotation marks.
The following code raises the "SyntaxError: invalid character" error:
# 🚫 SyntaxError: invalid character '´' (U+00B4)
book_title = ´Head First Python´
All we need to do is to retype the quotation marks with our English keyboard:
# ✅ Correct:
book_title = 'Head First Python'
An invalid comma ","
instead of ","
: Non-English keyboards (such as Chinese or Persian) usually have different forms of commas, which you can't use as delimiters in Python - even though they are totally fine commas.
This can happen in function arguments, tuples, lists, dictionaries, etc.
# 🚫 SyntaxError: invalid character ',' (U+FF0C)
book = { 'title': 'Head First Python','author': 'Paul Barry'}
As you can see, the above comma is slightly smaller than a normal comma. To fix the error, we retype it like so:
# ✅ Correct:
book = { 'title': 'Head First Python', 'author': 'Paul Barry'}
Using "—"
instead of "-"
when subtracting numbers: This one rarely finds its way into your program, but it's worth watching when copying code from another source.
Here's an example:
a = 12
b = 76
# 🚫 SyntaxError: invalid character '—' (U+2014)
print(a — b)
And swapping it with a hyphen fixes the error instantly:
a = 12
b = 76
# ✅ Correct:
print(a - b)
You might also get a similar SyntaxError due to non-printing characters hidden in the code. Debugging such errors are more tricky than printed characters. Luckily, Python will explicitly mention it's a non-printing character, and there are tools to detect them.
Alright, I think it does it! I hope this short guide helped you fix your problem.
Thanks for reading!
❤️ You might like:
Posted on February 1, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.