Python:多行格式输出中的空格

时间:2017-04-18 20:15:43

标签: python pep8

尝试通过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)$

1 个答案:

答案 0 :(得分:0)

由于您使用的是""",因此您应该在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