簡體   English   中英

刪除字符串中的“\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
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM