how to fix "SyntaxError: invalid non-printable character" in Python
Tags: python, syntaxerror, programming
Published: 2023-02-01 22:13:02

> ✋ **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-non-printable-character-in-python) for a 💯 user experience. _~reza_

The error “SyntaxError: invalid non-printable character” in Python happens once Python encounters an invalid non-printing character (according to Python’s [syntax](https://www.decodingweb.dev/how-to-learn-a-programming-language#language-syntax-and-semantics)) in your statements. 

Non-printing characters might not be visible in your code editor, and you might not notice them until you run the code. Having characters such as _zero-width space_ (with Unicode code point `U+200B`) and _byte-order mark_ (Unicode code point `U+FEFF`) are the two common causes of this error.

Invalid non-printable characters may end up in your code if you’ve copied a code snippet from a web page, a PDF document, or another formatted text.

Here’s what the error looks like:


```
File /dwd/sandbox/test.py, line 1
  ​​​​​​​​​​​f = 12
  ^
SyntaxError: invalid non-printable character U+200B
```

And here's what it feels like: 🥴

Luckily, this error message indicates where these invisible characters reside. Removing these characters fixes the issue instantly.

## How to fix the "SyntaxError: invalid non-printable character" error in Python

Any invalid non-printable character lead to the "SyntaxError: invalid non-printable character" error. 

To fix it:

1. **First,** inspect the error to see which line is causing the error.
2. **Then,** copy that line into a [non-printing character viewer tool](https://www.soscisurvey.de/tools/view-chars.php).
3. **Finally,** remove those unwanted characters.

Having the following characters in your code is the most common reason behind this syntax error:

<u>1. Zero-width space characters (ZWSP)</u>
<u>2. A Byte-order mark character (BOM)</u>

Let's explore each scenario with some examples.

**Zero-width space characters (ZWSP):** A zero-width space character (ZWSP) is a non-printing character used in digital typesetting to define word boundaries (like a space character) in systems and languages without visible spacing between words.

On the other hand, it's a character designed for machines, not us.

You can even add a zero-width space to your HTML document using the `&ZeroWidthSpace` entity. Although it's not visible on the page, it'll be included if somebody copies the text.

So if you copy a code from the Internet that contains ZWSP characters, you'd also copy them to your code editor.


```html
&ZeroWidthSpace;&ZeroWidthSpace;f=12
```

And if you try that on Python:

```python
f=12
```

You'll get the error:


```
File /dwd/sandbox/test.py, line 1
  ​​f=12
  ^
SyntaxError: invalid non-printable character U+200B
```

To fix the error, you can paste the code into [this tool](https://www.soscisurvey.de/tools/view-chars.php) to see the non-printing characters. Just remember to remove them before copying them back to your code editor.

Problem solved!

**A byte-order mark character:** A byte-order mark is an optional character (with Unicode code point `U+FEFF`) used to provide meta information to a script parsing the text. For instance, to signal the byte order or the Unicode character encoding used.

Windows is known to include this character to identify a file as a UTF-encoded file. However, it confuses other systems that assume all files UTF-encoded.

So if you get this error on Windows, all you need to do is to save your file as UTF with no BOM.

Alright, I think it does it! I hope this short guide helped you fix your problem.

Thanks for reading!