Skip to main content

How to Resolve "SyntaxError: invalid character" in Python

The SyntaxError: invalid character error in Python occurs when your code contains characters that Python doesn't recognize as valid syntax. This often happens due to copy-pasting code from websites, using a keyboard layout with non-standard punctuation, or having invisible Unicode characters in your code.

This guide explains the common causes and how to fix them.

Common Causes of SyntaxError: invalid character​

The error message usually points to the specific line and character causing the problem. Here are the most frequent culprits:

Non-Standard Quotes​

Sometimes, copying code from websites or documents can introduce "smart quotes" or other non-standard quotation marks that Python doesn't recognize.

# ⛔️ SyntaxError: invalid character 'β€˜' (U+2018)
name = β€˜Tom Nolanβ€˜ # Incorrect quotes

Non-Standard Commas​

Similar to quotes, using commas from different languages or character sets can cause problems.

# ⛔️ SyntaxError: invalid character ',' (U+FF0C)
names = ['Alice','Tom'] # Incorrect comma

Non-Standard Minus Signs​

There are multiple Unicode characters that look like minus signs, but only the standard hyphen-minus (-, U+002D) is valid in Python code for subtraction.

int_1 = 100
int_2 = 50

# ⛔️ SyntaxError: invalid character 'β€”' (U+2014)
result = int_1 β€” int_2 # Incorrect minus sign (em dash)

Invisible Characters​

Invisible Unicode characters (like Zero Width Space, U+200B) can be present in your code, especially after copy-pasting, and cause syntax errors. These are the hardest to spot because they don't display visibly.

# ⛔️ SyntaxError: invalid non-printable character U+200B
a_dict = ​{"Tom Nolan": [{"id": 1}] # Invisible character before the brace
  • The error message may not point the character directly, but a general position on the line.

How to Fix the Error​

Rewrite the Line​

The most reliable solution is to manually retype the entire line where the error occurs. Don't copy and paste; type it out. This ensures you're using the correct characters from your keyboard. Pay close attention to:

  • Quotes: Use standard single quotes (') or double quotes (").
  • Commas: Use the standard comma (,).
  • Minus signs: Use the standard hyphen-minus (-).
  • Other punctuation: Check the brackets, parentheses and operators.
# After rewriting the line with correct quotes:
name = 'Tom Nolan' # Correct
names = ['Alice', 'Tom'] # Correct
result = int_1 - int_2 # Correct
a_dict = {"Tom Nolan": [{"id": 1}]} # Correct

Use a Unicode Character Inspector​

If you suspect invisible characters, use an online tool to inspect your code for non-printable Unicode characters. There are many websites that do this; search for "unicode character inspector" or "invisible character checker". These tools will highlight any unusual characters.

You can compare the Unicode code points using the ord() function:

print(ord(','))    # Output: 65292 (FULLWIDTH COMMA)
print(ord(',')) # Output: 44 (COMMA)

print(ord('β€”')) # Output: 8212 (EM DASH)
print(ord('-')) # Output: 45 (HYPHEN MINUS)

Check your keyboard​

Make sure your keyboard language/layout is set correctly.

This can prevent accidentally typing the wrong characters, especially punctuation.