<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pentestcurry</title>
    <description>The latest articles on DEV Community by pentestcurry (@pentestcurry).</description>
    <link>https://dev.to/pentestcurry</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4243328%2Fb0753ab2-9fa3-4c8c-b91e-d8fc9eb07404.png</url>
      <title>DEV Community: pentestcurry</title>
      <link>https://dev.to/pentestcurry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pentestcurry"/>
    <language>en</language>
    <item>
      <title>How to Fix "SyntaxError: invalid non-printable character" in Python</title>
      <dc:creator>pentestcurry</dc:creator>
      <pubDate>Fri, 13 Nov 2026 17:01:51 +0000</pubDate>
      <link>https://dev.to/pentestcurry/how-to-fix-syntaxerror-invalid-non-printable-character-in-python-2ack</link>
      <guid>https://dev.to/pentestcurry/how-to-fix-syntaxerror-invalid-non-printable-character-in-python-2ack</guid>
      <description>&lt;h1&gt;
  
  
  How to Fix "SyntaxError: invalid non-printable character" in Python
&lt;/h1&gt;

&lt;p&gt;Copy-pasted code from ChatGPT, a website, or a PDF throws &lt;code&gt;SyntaxError: invalid non-printable character U+00A0&lt;/code&gt; — and you can't see anything wrong. Here's what that character is, why Python rejects it, and three ways to fix it in under a minute.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the error actually means
&lt;/h2&gt;

&lt;p&gt;Python's tokenizer only accepts a specific set of characters outside strings. &lt;code&gt;U+00A0&lt;/code&gt; is a &lt;strong&gt;non-breaking space&lt;/strong&gt; — it renders exactly like a normal space (U+0020), but it's a different Unicode code point. When Python hits it outside a string literal, it refuses to tokenize the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SyntaxError: invalid non-printable character U+00A0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same family of errors appears for other invisible characters:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Code point&lt;/th&gt;
&lt;th&gt;Character&lt;/th&gt;
&lt;th&gt;Typical source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;U+00A0&lt;/td&gt;
&lt;td&gt;Non-breaking space&lt;/td&gt;
&lt;td&gt;Word processors, websites, chat apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;U+200B&lt;/td&gt;
&lt;td&gt;Zero-width space&lt;/td&gt;
&lt;td&gt;AI chat output, some pastes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;U+201C / U+201D&lt;/td&gt;
&lt;td&gt;Curly quotes&lt;/td&gt;
&lt;td&gt;Word processors, AI drafts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;U+2013 / U+2014&lt;/td&gt;
&lt;td&gt;En/em dash&lt;/td&gt;
&lt;td&gt;Word processors, AI drafts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;U+202A–U+202E&lt;/td&gt;
&lt;td&gt;Bidi controls&lt;/td&gt;
&lt;td&gt;Copied RTL text, rare&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The kicker: your editor displays them invisibly, the error points at a line that "looks fine," and retyping works only if you find the right line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 1 — Find the exact character (30 seconds)
&lt;/h2&gt;

&lt;p&gt;Paste the failing code into a viewer that names each code point at its line and column. I built a free one that runs entirely in your browser — nothing is uploaded: &lt;a href="https://aiwatermarkremovers.com/code-cleaner" rel="noopener noreferrer"&gt;the code paste cleaner&lt;/a&gt;. It highlights the invisible character at the exact caret position and swaps in the correct ASCII equivalent (regular space, straight quotes, plain dashes), then gives you clean code to copy back.&lt;/p&gt;

&lt;p&gt;The companion &lt;a href="https://aiwatermarkremovers.com/text-integrity-checker" rel="noopener noreferrer"&gt;invisible Unicode checker&lt;/a&gt; does the same for prose and shows every suspicious code point in the text — useful for AI-assisted drafts where you want to see &lt;em&gt;everything&lt;/em&gt; hiding in there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 2 — Replace the character with Python itself
&lt;/h2&gt;

&lt;p&gt;If you know it's U+00A0, a one-liner rewrites the whole file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt;

&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script.py"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\u00a0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the broader family (curly quotes, dashes, zero-width), normalize explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;unicodedata&lt;/span&gt;

&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script.py"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;replacements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"\u00a0"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\u200b"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"\u201c"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\u201d"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\u2018"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\u2019"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"\u2013"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\u2014"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"-"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;good&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;replacements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;good&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;unicodedata.normalize("NFKC", text)&lt;/code&gt; also converts many of these — but it's aggressive: it changes characters you may want kept (like full-width symbols), so check the diff after.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 3 — Prevent it next time
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Retype, don't paste&lt;/strong&gt;, code from chat interfaces and PDFs — or paste into a plain-text editor first (Notepad, &lt;code&gt;nano&lt;/code&gt;), then into your IDE&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configure your editor to reveal them&lt;/strong&gt;: VS Code's &lt;code&gt;editor.renderWhitespace: "boundary"&lt;/code&gt; shows unusual spaces; searching with regex &lt;code&gt;\u00A0&lt;/code&gt; finds them file-wide&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run a linter before executing&lt;/strong&gt;: &lt;code&gt;ruff&lt;/code&gt; and &lt;code&gt;flake8&lt;/code&gt; flag suspicious characters with the code point named, which is much clearer than Python's runtime error&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why this happens more with AI-assisted coding
&lt;/h2&gt;

&lt;p&gt;When you copy code out of chat interfaces, the copy can carry the chat's formatting characters — non-breaking spaces between tokens, curly quotes in strings, occasionally zero-width characters between letters. The code &lt;em&gt;looks&lt;/em&gt; right because every one of these renders invisibly or near-invisibly. The error is Python doing its job: refusing characters that were never legal in source code.&lt;/p&gt;

&lt;p&gt;The fix is always the same: make the invisible visible, replace with the ASCII equivalent, keep the logic identical.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If you want the checker mentioned above: &lt;a href="https://aiwatermarkremovers.com/code-cleaner" rel="noopener noreferrer"&gt;code-cleaner&lt;/a&gt; for syntax-breaking characters, and the npm CLI &lt;a href="https://www.npmjs.com/package/strip-invisible" rel="noopener noreferrer"&gt;&lt;code&gt;strip-invisible&lt;/code&gt;&lt;/a&gt; (&lt;code&gt;npx strip-invisible --check script.py&lt;/code&gt;) if you'd rather stay in the terminal. Both free, no account.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
