How to fix "SyntaxError: invalid character" in Python
Tags: python, syntaxerror, programming
Published: 2023-02-01 22:35:36

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/solved-syntaxerror-invalid-character-in-python) 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)
```

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. <u>A pair of invalid quotation marks like `´´`</u>
2. <u>An invalid comma `"，"` instead of `","`</u>
3. <u>Using `"—"` instead of `"-"` when subtracting numbers</u>

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:


```python
# 🚫 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:


```python
# ✅ Correct:
book_title = 'Head First Python'
```

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


```python
# 🚫 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:


```python
# ✅ Correct:
book = { 'title': 'Head First Python', 'author': 'Paul Barry'}
```

**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:

```python
a = 12
b = 76

# 🚫 SyntaxError: invalid character '—' (U+2014)
print(a — b)
```

And swapping it with a hyphen fixes the error instantly:


```python
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!

**❤️ You might like:**

- [SyntaxError: unterminated string literal in Python](https://www.decodingweb.dev/solved-syntaxerror-unterminated-string-literal-in-python)
- [SyntaxError: EOL while scanning string literal in Python](https://www.decodingweb.dev/solved-syntaxerror-eol-while-scanning-string-literal-in-python)
- [SyntaxError: cannot assign to expression here in Python](https://www.decodingweb.dev/solved-syntaxerror-cannot-assign-to-expression-here-in-python)
- [SyntaxError: cannot assign to literal here in Python](https://www.decodingweb.dev/solved-syntaxerror-cannot-assign-to-literal-here-in-python)
- [SyntaxError: invalid decimal literal in Python (Solved)](https://www.decodingweb.dev/python-syntaxerror-invalid-decimal-literal-fix)