Python error in Console but not in File: unexpected character after line continuation character
<p>I've got a Python script which has a class defined with this method:</p>

<pre><code>@staticmethod
def _sanitized_test_name(orig_name):
    return re.sub(r'[`ââ\"]*', '', re.sub(r'[\r\n\/\:\?\&lt;\&gt;\|\*\%]*', '', orig_name.encode('utf-8')))
</code></pre>

<p>I'm able to run the script from the command prompt just fine, without any issues. But when I paste the code of the full class in the console, I get the <code>SyntaxError: unexpected character after line continuation character</code>:</p>

<pre><code>&gt;&gt;&gt; return re.sub(r'[`ââ\"]*', '', re.sub(r'[\r\n\/\:\?\&lt;\&gt;\|\*\%]*', '', orig_name.encode('utf-8')))
  File "&lt;stdin&gt;", line 1
    return re.sub(r'[``'\"]*', '', re.sub(r'[\r\n\/\:\?\&lt;\&gt;\|\*\%]*', '', orig_name.encode('utf-8')))
                                                                                                    ^
SyntaxError: unexpected character after line continuation character
</code></pre>

<p>If I skip that method while pasting, it works. Note that there is a difference in what my original line is and what's shown for the error: <code>r'[`ââ\"]*'</code> vs <code>r'[``'"]*'</code>. Replacing that with <code>ur'[`ââ"]*'</code> gives <code>SyntaxError: EOL while scanning string literal</code>.</p>

<p>It seems the Python console is seeing <code>â</code> that as a stylised <code>`</code> (backtick) and the <code>â</code> as a sytlised <code>'</code> (single quote). When I really mean the <a href="https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html" rel="nofollow">unicode open and close quotes</a>. I've got <code># -*- coding: utf-8 -*-</code> at the top of my script, which I paste into the console as well.</p>

<p>Focusing on just the expression causing the error <code>r'[`ââ"]*'</code>...</p>

<pre><code>&gt;&gt;&gt; r'[`ââ"]*'
  File "&lt;stdin&gt;", line 1
    r'[``'"]*'
             ^
SyntaxError: EOL while scanning string literal
&gt;&gt;&gt; ur'[`ââ"]*'  # with the unicode modifier
  File "&lt;stdin&gt;", line 1
    ur'[``'"]*'
              ^
SyntaxError: EOL while scanning string literal
</code></pre>

<p>If the terminal I'm in doesn't accept unicode input, that interpretation of the unicode chars from <code>â</code> to <code>`</code> and <code>â</code> to <code>'</code>, occurs.</p>

<p>So the workaround is to split the regex and <strong>use <a href="https://docs.python.org/2/library/functions.html#unichr" rel="nofollow"><code>unichr()</code></a></strong> with the corresponding codes for the two quotes, 2018 and 2019:</p>

<pre><code>&gt;&gt;&gt; r'[`' + unichr(2018) + unichr(2019) + r'"]*'
u'[`\u07e2\u07e3"]*'
</code></pre>

<p>(And the raw string modifier <code>r''</code> probably isn't required for this particular regex.)</p>

python console