【问题标题】:Python linebreak and indent with 1 line of code?Python换行和缩进1行代码?
【发布时间】:2018-01-28 04:19:38
【问题描述】:

在 Python 中,是否可以打印带有换行符和缩进的字符串,但只能使用 1 行代码? (见下文)

    Wow!
        This is on a new line!

【问题讨论】:

  • 我强烈建议阅读 Python 字符串:print("Wow!\n\tThis is on a new line!")
  • print('\tWow!\n\tThis is on a new line!')?
  • 确保您以@NiemaMoshiri 的身份行事,专门查找术语“转义字符”。

标签: python indentation line-breaks


【解决方案1】:

在 python 中,\n 将生成一个新行,\t 将缩进一行(就像键盘上的 Tab 键一样)。所以以下应该工作:

print('\tWow!\n\t\tThis is on a new line!')

请注意我与问题下方的 cmets 有何不同,在第二行使用 \t 两次以将其缩进比其上方的行更远

【讨论】:

    猜你喜欢
    • 2015-07-10
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多