如何使用 strip() 从句子中删除 \n 字符

How to remove \n character from a sentence using strip()

我正在尝试使用 strip() 命令从句子中删除 \n 个字符,但它似乎不起作用。

str1 = "Hello World \n, I\n am \nhere"
print(str1.strip())

输出

Hello World 
, I
 am 
here

strip() 仅从开头和结尾删除白色space 字符(换行符、制表符、space)。要在 \n 之间删除,请使用 replace()

>>> str1 = "Hello World \n, I\n am \nhere"
>>> print(str1.replace('\n', ''))
Hello World , I am here
>>>