[Solved] SyntaxError: invalid character in Python

Updated Sep 22, 2023 ⤳ 3 min read

The error “SyntaxError: invalid character” occurs once Python encounters an invalid character (according to Python’s syntax) in a statement. 

You may have invalid characters in a line of 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 message 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. 

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

How to fix it?

As mentioned earlier, this infamous syntax error occurs due to any character Python considers invalid.

To fix it:

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

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 with some examples.

1. A pair of invalid quotation marks like ´´:  All string literals must be quoted in Python, with the standard single, double, or triple quotation marks.

The following code raises the "invalid character" error because we're using non-standard quotation marks:


# 🚫 Python SyntaxError: invalid character '´' (U+00B4)
book_title = ´Head First Python´

All we need to do is to quote characters with our English keyboard enabled:


# ✅ 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)

Swapping this character with a hyphen fixes the error instantly:


a = 12
b = 76

# ✅ Correct:
print(a - b)

You might also get a similar syntax error due to non-printing characters hidden in the code. Debugging such errors are trickier than printed characters. Luckily, Python will explicitly mention it's a non-printing character that's causing the issue, 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!

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

`
Exit mobile version