SyntaxError: invalid character in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 17, 2023
3 min

banner

# SyntaxError: invalid character in Python [Solved]

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.

syntaxerror invalid character

Here is an example of how the error occurs.

main.py
# ⛔️ SyntaxError: invalid character '‘' (U+2018) name = ‘Bobby Hadz‘

invalid quote characters

Notice that the screenshot with the error message shows exactly where the error occurred with an arrow pointing at the quote character.

The example doesn't use single quotes. Instead, it uses some other quote character that is not supported.

# Rewrite the line where the error occurred

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).

main.py
# ✅ using regular quotes instead of apostrophes name = 'Bobby Hadz' print(name)

using regular quotes instead of apostrophes

Now we used single quotes, so the error is resolved.

Comparing the non-standard quote to the standard quote returns False.

main.py
non_standard_quote = "‘" standard_quote = "'" # 👇️ False print(non_standard_quote == standard_quote)

comparing non standard quote to standard quote

The non-standard quote character is distinct and cannot be parsed by Python.

# The character that causes the error might be invisible

The character that causes the error might be invisible.

Here is an example.

main.py
# ⛔️ SyntaxError: invalid non-printable character U+200B a_dict ={"Bobby Hadz": [{"id": 1}]

non printable unicode character

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.
However, the best way to solve the error is to look at the error message and rewrite the lines on which the error occurred.

# Another example of how the error occurs

Here is another example.

main.py
# ⛔️ 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.

syntaxerror invalid character 2

Once I rewrite the code to use a regular comma, the error is solved.

main.py
# ✅ using a regular comma names = ['Alice','Bob']
The best way to solve the error is to simply rewrite your code because there might be non-printable (invisible) Unicode characters that cause your issue.

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.

main.py
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.

# Making sure your keyboard is set to the correct language

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.

# The error also occurs in mathematical operations

Here is another example of how the error occurs.

main.py
int_1 = 100 int_2 = 50 # ⛔️ SyntaxError: invalid character '—' (U+2014) result = int_1 — int_2

non standard minus sign

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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.