[Solved] SyntaxError: invalid character in Python

A hand holding the Python logo
Photo by RealToughCandy.com, source: Pexels

🚫 SyntaxError: invalid character in Python

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:

  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.

1. 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'

2. 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'}

3. 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!

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

Disclaimer: This post might contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.