>> t = "first%s\n" >>> t = t %("second") >>> print t firstsecond 无论如何我可以保留最后的“\n”并得到“firstsecond\n”-6ren">
gpt4 book ai didi

python - 保留 "\n"

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:37 27 4
gpt4 key购买 nike

>>> 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

关于python - 保留 "\n",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com