保留"\n"

1 python

>>> t = "first%s\n"
>>> t = t %("second")
>>> print t
firstsecond
Run Code Online (Sandbox Code Playgroud)

无论如何,我可以保留最后的"\n"并输出"firstsecond \n"作为输出?

yan*_*yan 7

你需要逃避斜线

>>> t = "first%s\\n"
>>> t = t %("second")
>>> print t
Run Code Online (Sandbox Code Playgroud)

或使用原始字符串:

>>> t = r"first%s\n"
>>> t = t %("second")
>>> print t   
Run Code Online (Sandbox Code Playgroud)