删除字符串中的“\n”时如何保存“\\n”

How to save " \\n " when removing " \n " in string

>>> b = 'first \n second \n third \n forth \n fifth \n'
>>> print(b)
first \n second 
 third \n forth 
 fifth \n

b 想要的结果,
打印(结果)
first \n second third \n forth fifth \n
结果 = 'first \n second third \n forth fifth \n'
怎么做?

\n一个转义字符,表示换行符,而\n是一个反斜杠和一个字母n,删除\n:

不受影响
>>> b = 'first \n second \n third \n forth \n fifth \n'
>>> b.replace('\n', '')
'first \n second  third \n forth  fifth \n'

这个例子可能会让你意识到它们之间的区别:

>>> len('\n')
1
>>> list('\n')
['\n']
>>> len('\n')
2
>>> list('\n')
['\', 'n']