如何从这个示例字符串中删除\n?(在 Python 3 中)
'Word 1\nWord 2'
我试着用 .带子 但它只能在字符串的开头或结尾删除 \n... ... 我也试过
string.replace('\\n', '')
但似乎也没有用...
replace
对我来说是有效的,请确保你做了以下操作。
a = 'Word 1\nWord 2'
a = a.replace("\n", "") #instead of just a.replace("\n", "")
返回: 'Word 1Word 2'
你使用的是双 \\
在这里的新行-> string.replace('\\n', '')
用'"\n "代替
将此改为:
string.replace('\\n', '')
替换为 "这个"。
string = string.replace('\n', '') //you used double slashes for some reason
当你使用双斜线时,它认为你要替换这两个字符,而它们是按顺序排列的。\n
. 但你要替换的是一个换行符,所以你不需要加上额外的斜杠。
y=""
for x in 'Word 1\nWord 2':
if(x != "/n"):
y=y+x
print(y)
你可以使用这段代码,它将打印
Word1
Word2
希望对大家有所帮助。