我有几个带有\
的字符串。我尝试通过使用类似的替换功能不使用转义字符:
string.replace("\\", "\\\\")
它不起作用。当我直接将string.replace("\n", "\\n")
用于\n
时,它可以工作。有没有简单可行的解决方案?
重要的是,replace并不作用于变量,而是返回应用了替换的副本:
this = "string\\string"
this.replace("\\", "")
print(this)
>"string\\string"
如果您想替换原始字符串,
this = this.replace("\\", "")
使用raw strings。
r"This \ string \ includes '\'s"