DEV Community

Cover image for how to fix "SyntaxError: invalid non-printable character" in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

how to fix "SyntaxError: invalid non-printable 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 non-printable character” in Python happens once Python encounters an invalid non-printing character (according to Python’s syntax) 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
Enter fullscreen mode Exit fullscreen mode

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.
  3. Finally, remove those unwanted characters.

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

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

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.

​​f=12
Enter fullscreen mode Exit fullscreen mode

And if you try that on Python:

f=12
Enter fullscreen mode Exit fullscreen mode

You'll get the error:

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

To fix the error, you can paste the code into this tool 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!

Top comments (0)