DEV Community

Cover image for How to fix "SyntaxError: invalid character" in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

How to fix "SyntaxError: invalid character" in Python

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)
Enter fullscreen mode Exit fullscreen mode

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:

  1. First: Inspect the error and find the invalid character.
  2. Then: Retype it with your English keyboard.

Below are the most common characters that cause the issue:

  1. A pair of invalid quotation marks like ´´
  2. An invalid comma "," instead of ","
  3. 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´
Enter fullscreen mode Exit fullscreen mode

All we need to do is to retype the quotation marks with our English keyboard:

# ✅ Correct:
book_title = 'Head First Python'
Enter fullscreen mode Exit fullscreen mode

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'}
Enter fullscreen mode Exit fullscreen mode

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'}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

And swapping it with a hyphen fixes the error instantly:

a = 12
b = 76

# ✅ Correct:
print(a - b)
Enter fullscreen mode Exit fullscreen mode

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:

Top comments (0)