[Solved] SyntaxError: invalid non-printable character in Python

Updated Sep 22, 2023 ⤳ 4 min read

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

And here's what it feels like: 🥴

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

🎧 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! 🎵💻🚀

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

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

And if you try that on Python:


# 🚫 SyntaxError: invalid non-printable character U+200B
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 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!

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.

`