Python: Replace Slashes
<p>I got some unicode with percent signs I want to change to backslashes. I know you need an additional backslash as an escape sequence, but when I did this <code>replace()</code> gave me two backslashes:</p>

<pre><code>&gt;&gt;&gt; s ="%20%u200F%u05D1%u05E8%u05DB%u05EA%u200F%20%u200F%u05D4%u05E8%u05D9%u05D7%u200F%20%u200F%u05D5%u05D1%u05E8%u05DB%u05EA%u200F%20%u200F%u05D4%u05D5%u05D3%u05D0%u05D4"
&gt;&gt;&gt; s.replace("%","\")
File "&lt;stdin&gt;", line 1
s.replace("%","\")                  
SyntaxError: EOL while scanning string literal

&gt;&gt;&gt; s.replace("%","\\")
'\\20\\u200F\\u05D1\\u05E8\\u05DB\\u05EA\\u200F\\20\\u200F\\u05D4\\u05E8\\u05D9\\u05D7\\u200F\\20\\u200F\\u05D5\\u05D1\\u05E8\\u05DB\\u05EA\\u200F\\20\\u200F\\u05D4\\u05D5\\u05D3\\u05D0\\u05D4'
</code></pre>

<p>The <code>'r'</code> qualifyer isn't working either. Any ideas?</p>

<p>That is just python invoking the string <code>__repr__</code> because you're in the REPL and the <code>__repr__</code> escapes the <code>\</code> character giving you the output you see.</p>

<p>Using <code>print</code>, which will use the strings <code>__str__</code>, will get you the right output:</p>

<pre><code>print(s.replace("%","\\"))    
\20\u200F\u05D1\u05E8\u05DB\u05EA\u200F\20\u200F\u05D4\u05E8\u05D9\u05D7\u200F\20\u200F\u05D5\u05D1\u05E8\u05DB\u05EA\u200F\20\u200F\u05D4\u05D5\u05D3\u05D0\u05D4
</code></pre>

<p>That is, the replacement happens just fine.</p>

python string