Python 如何将一行输出更改为多行
我在.txt文件中有这一行:Python 如何将一行输出更改为多行,python,string,output,Python,String,Output,我在.txt文件中有这一行: I just got home.\nDid you make lunch?\nOh, I see... 这只是一行,它的格式是这样的,用于一个游戏,所以它完全是这样的,带有\n的 我需要的是,当我打印它时,在我将它存储到变量或列表中之后,根据。像这样: I just got home.\n Did you make lunch?\n Oh, I see... 我想不出一种方法来修改输出,以便在程序显示每一行后生成新行\n。如何执行此操作?使用str.replac
I just got home.\nDid you make lunch?\nOh, I see...
这只是一行,它的格式是这样的,用于一个游戏,所以它完全是这样的,带有\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...
@思百吉谢谢