[Solved] SyntaxError: invalid non-printable character in Python
🚫 SyntaxError: invalid non-printable character
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:
- First, inspect the error to see which line is causing the error.
- Then, copy that line into a non-printing character viewer tool.
- Finally, remove those unwanted characters.
Having the following characters in your code is the most common reason behind this syntax error:
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.
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.
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!
If you read this far, you can tweet to the author to show them you care. Tweet a Thanks
Never miss a guide like this!
Join the DWD mailing list for your weekly dose of knowledge! This mailing list is for doers and thinkers. If you're a curious person, you might find it useful, just as 2,000 other devs do!
Disclaimer: This post might contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.