Last updated: Apr 8, 2024
Reading time·3 min
The Python "SyntaxError: invalid character" occurs when we use an invalid character in our code, e.g. from copy-pasting.
To solve the error, look at the line where the error message is pointing, rewrite the line and remove any non-printable Unicode characters.
Here is an example of how the error occurs.
# ⛔️ SyntaxError: invalid character '‘' (U+2018) name = ‘Bobby Hadz‘
The example doesn't use single quotes. Instead, it uses some other quote character that is not supported.
The best way to solve the error is to rewrite the line that the error message points to (especially if you copy-pasted it from somewhere).
# ✅ Using regular quotes instead of apostrophes name = 'Bobby Hadz' print(name)
Now we used single quotes, so the error is resolved.
Comparing the non-standard quote to the standard quote returns False
.
non_standard_quote = "‘" standard_quote = "'" # 👇️ False print(non_standard_quote == standard_quote)
The non-standard quote character is distinct and cannot be parsed by Python.
The character that causes the error might be invisible.
Here is an example.
# ⛔️ SyntaxError: invalid non-printable character U+200B a_dict = {"Bobby Hadz": [{"id": 1}]
There is a non-printable Unicode character right before the opening curly brace of the dictionary.
You can paste your code into a tool like the following to view the non-printable Unicode characters.Here is another example.
# ⛔️ SyntaxError: invalid character ',' (U+FF0C) names = ['Alice','Bob']
We didn't use a regular comma which caused the error.
The error message shows exactly where the error occurred.
Once I rewrite the code to use a regular comma, the error is solved.
# ✅ Using a regular comma names = ['Alice','Bob']
Especially if you copy-pasted the code from somewhere and your IDE doesn't take care of removing these characters, it's very hard to track them down.
You can try checking for non-printable Unicode characters in your code by pasting your code in a tool like this one.
However, the best way to solve the error is to look at the line of code the error message points to and rewrite it completely.
You can also use the ord()
function to compare the non-standard character to
the standard one.
print(ord(',')) # 👉️ 65292 print(ord(',')) # 👉️ 44
The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.
The Unicode code points of the non-standard comma and the standard one are distinct.
Make sure your keyboard is set in the correct language.
Using a punctuation character from a different language often causes the error.
For example, Python might not be able to interpret the single or double quote from language X.
Here is another example of how the error occurs.
int_1 = 100 int_2 = 50 # ⛔️ SyntaxError: invalid character '—' (U+2014) result = int_1 — int_2
I used a non-standard minus sign in the example which caused the error.
To solve the error, switch to the correct language and rewrite the minus -
sign.
int_1 = 100 int_2 = 50 # ✅ Works result = int_1 - int_2 print(result) # 👉️ 50
The code sample works as intended now that a regular minus -
sign is used.