简体   繁体   English

"Python 替换字符串中的 \\n"

[英]Python replace \n in a string

How can I replace \\n in my string?如何在我的字符串中替换 \\n? For example: test = Hello\\nHello\\nHello If I would try test = test.replace("","") it would not work because \\n is automatically a new line so is there a trick to do this?例如: test = Hello\\nHello\\nHello 如果我尝试 test = test.replace("","") 它将不起作用,因为 \\n 自动是一个新行,所以有什么技巧可以做到这一点?

So again test = test.replace("","") is not working所以再次 test = test.replace("","") 不起作用

"

Your question is not so clear, but from what I understood you should write:你的问题不是很清楚,但根据我的理解你应该写:

test = "Hello\nHello\nHello"
test = test.replace("\n", "")

You can directly do你可以直接做

test = 'Hello\nHello\nHello'
result = test.replace('\n', '')
print(result)

HelloHelloHello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM