将字符串中的反斜杠替换为 Python 3.8

Replace backslash in String with Python 3.8

我查找了几种从 Python 中的字符串中提取反斜杠的方法,但其中 none 对我有用。我的字符串如下所示:

s = "This is just a \ test \ string"


我尝试了以下方法(因为 Whosebug 上有几个答案):

s.replace('\', "")

但这不起作用。我得到以下输出:

print(s)
>>> "This is just a \ test \ string"

谢谢!

这是因为 string.replace 不会就地更改字符串。以下应该有效:

>>> s = "This is just a \ test \ string"
>>> s = s.replace('\', "")
>>> s
'This is just a  test  string'