【问题标题】:How to change a 1 line output to multiple lines如何将 1 行输出更改为多行
【发布时间】:2017-06-07 14:01:04
【问题描述】:

我在 .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 后修改输出以换行的方法。我该怎么做?

【问题讨论】:

标签: python string output


【解决方案1】:

使用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...

【讨论】:

    猜你喜欢
    • 2020-12-16
    • 2018-10-30
    • 1970-01-01
    • 2021-08-04
    • 2022-01-05
    • 2011-08-20
    • 2014-12-10
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多