擅长:python、mysql、java
<p>另一种选择:使用Python的原始字符串:</p>
<pre><code>>>> "'hello'".replace("'", "\'")
>>> "'hello'"
>>>
>>> "'hello'".replace("'", "\\'")
>>> "\\'hello\\'"
>>>
>>> "'hello'".replace("'", r"\'") # <- Note the starting r
>>> "\\'hello\\'"
</code></pre>