【问题标题】:Python: space in multiple-line format output [duplicate]Python:多行格式输出的空格[重复]
【发布时间】:2017-09-14 20:07:45
【问题描述】:

尝试通过 pep8 重构代码。

代码:

    print """Exception when sending email to: {0},
        from: {1}, subject: {2}, e: {3}""".format(to, fr, subject, e)

输出:

Exception when sending email to: to,
                from: fr, subject: subject, e: Invalid URL 'http//127.0.0.1:8000/'

如何删除上述输出中from 之前的空格?谢谢

更新

以下代码工作正常。我应该删除这篇文章吗?

 34             print ($
 35                 "Exception when sending email to: {0},"$
 36                 "from: {1}, subject: {2}, e: {3}").format(to, fr, subject, e)$

【问题讨论】:

  • 字符串字面量不需要在中间缩进。

标签: python pep8


【解决方案1】:

由于您使用的是""",因此您应该在this link 搜索术语“triple-quotes”

更改:

print """Exception when sending email to: {0},
        from: {1}, subject: {2}, e: {3}""".format(to, fr, subject, e)

收件人:

print """Exception when sending email to: {0}, from: {1}, subject: {2}, e: {3}""".format(to, fr, subject, e)

编辑:

如果需要保持行短,也可以使用\继续多行语句:

代码:

print("""Exception when sending email \
to: {0}, \
from: {1}, \
subject: {2}, \
e: {3}""".format('to', 'fr', 'subject', 'e'))

输出:

Exception when sending email to: to, from: fr, subject: subject, e: e

【讨论】:

  • 我需要保持每行包含少于 79 个字符。
  • 更新了我的答案...看看。
猜你喜欢
  • 2012-01-11
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 2016-10-08
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
相关资源
最近更新 更多