我在 .txt 文件中有这一行:
I just got home.\nDid you make lunch?\nOh, I see...
这只是一行,对于游戏来说它的格式是这样的,所以它就像这样,带有\n。
我需要的是,当我打印它时,将其存储到变量或列表中后,将其打印在多行上,尊重\n。像这样:
I just got home.\n
Did you make lunch?\n
Oh, I see...
我想不出一种方法来修改输出以在程序显示每个\n 后创建一个新行。我该怎么做?
最佳答案
使用 str.replace()
函数就足够了:
s="I just got home.\nDid you make lunch?\nOh, I see..."
print(s.replace("\n", "\\n\n"))
输出:
I just got home.\n
Did you make lunch?\n
Oh, I see...
关于python - 如何将 1 行输出更改为多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44414842/