2017-04-18 104 views
0

试图通过pep8重新分解代码。Python:多行格式输出空间

代码:

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之前删除的空间?由于

UPDATE

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

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

字符串文字不需要在中间缩进。 –

回答

0

由于您使用""",你应该寻找术语“三重引号”this link

变化:

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 
+1

我需要保持每行包含少于79个字符。 – BAE

+0

更新了我的答案...看一看。 –