【问题标题】:What is the Pythonic way to write a single line text in multiple lines [duplicate]在多行中编写单行文本的Pythonic方法是什么[重复]
【发布时间】:2018-02-21 14:50:31
【问题描述】:

我需要在 python 脚本中打印一个长的 const 语句,我们遵循“每行不超过 80 个字符”的规则。

我知道下面的方法会起作用,但有没有更好的方法来写这个?

print ("This is a reaaaaaaaaaaaaaaaaaaaaaaaally long "
       "sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx").format("")

或者,

print ("This is a reaaaaaaaaaaaaaaaaaaaaaaaally long " +
        "sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx")

或者,如果其中一个比另一个更可取。

编辑:为了更清楚,预期的输出是,

This is a reaaaaaaaaaaaaaaaaaaaaaaaally long sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx

【问题讨论】:

  • 虽然你的多行字符串有括号 - 你可以避免反斜杠、连接、格式化等
  • @user2263572 不,不是。那是关于多行字符串,这个问题是关于在多行中编写单行字符串。
  • 下面的答案来自我发布的链接。这是一个骗局。
  • @user2263572 我明白了。是的,你是对的。
  • 你的第一个例子没有违反任何 PEP 规则;你接受的答案确实。你的第一个例子很好,但请注意.format("") 是不必要的。

标签: python


【解决方案1】:

我认为这是一个更好的方法:

print(
"This is a reaaaaaaaaaaaaaaaaaaaaaaaally long "
"sentence"
)

实际上,你不需要任何东西来连接字符串。

【讨论】:

  • 啊,我错误地放了一个逗号,这就是它抱怨的原因。感谢您的回答。
猜你喜欢
  • 2016-08-31
  • 2011-02-28
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 2014-05-16
  • 2012-05-02
  • 2014-01-19
相关资源
最近更新 更多