【问题标题】:\n displaying in interactive shell\n 在交互式 shell 中显示
【发布时间】:2013-05-27 04:12:56
【问题描述】:

我是一个新手编剧,我正在使用交互式 shell -i 测试我的脚本。

这是我的脚本的一部分:

shelp = "min               shows the number of minutes\n\
sec               shows the number of seconds\n\
min + [NUMBER]    adds the number of minutes\n\
sec + [NUMBER]    adds the number of seconds"

当我以交互方式运行 shelp 时,它显示:

'min               shows the number of minutes\nsec               shows the number of seconds\nmin + [NUMBER]    adds the number of minutes\nsec + [NUMBER]    adds the number of seconds'

我不确定这是否是它应该做的,但有没有办法删除\n,同时仍然能够以交互方式运行我的脚本?

抱歉,如果这个问题已经得到解答,或者我的事实有误

【问题讨论】:

  • 如何打印 shelp 绑定?只有 print() 函数将\n 字符转换为结束行。
  • 你可以在每一行的末尾使用三引号"""for multiline strings"""而不是`\n`

标签: python interactive-shell


【解决方案1】:

听起来你在找print()

>>> print(shelp)
min               shows the number of minutes
sec               shows the number of seconds
min + [NUMBER]    adds the number of minutes
sec + [NUMBER]    adds the number of seconds
>>>

\n 代表一个换行符,如果您使用print(),它就是换行符。如果您只是自己键入shelp,交互式shell 会打印字符串的表示,它使用\n 来显示换行符的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2017-07-15
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多