Skip to main content
 首页 » 编程设计

python之保留 "\n"

2025年05月04日9powertoolsteam

>>> t = "first%s\n" 
>>> t = t %("second") 
>>> print t 
firstsecond 

无论如何我可以保留最后的“\n”并得到“firstsecond\n”作为输出吗?

请您参考如下方法:

你需要转义斜杠

>>> t = "first%s\\n" 
>>> t = t %("second") 
>>> print t 

或使用原始字符串:

>>> t = r"first%s\n" 
>>> t = t %("second") 
>>> print t