[英]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
result = '第一個\n第二個第三個\n第四個第五個\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']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.