如何在Python中进行换行(换行)? [英] How can I do a line break (line continuation) in Python?

查看:567
本文介绍了如何在Python中进行换行(换行)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一长行代码,我想在多行中分解.我使用什么,语法是什么?

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?

例如,添加一串字符串,

For example, adding a bunch of strings,

e = 'a' + 'b' + 'c' + 'd'

将其分为两行:

e = 'a' + 'b' +
    'c' + 'd'

推荐答案

哪一行?您只需在下一行就有参数就可以了,没有任何问题:

What is the line? You can just have arguments on the next line without any problems:

a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, 
            blahblah6, blahblah7)

否则,您可以执行以下操作:

Otherwise you can do something like this:

if a == True and \
   b == False

查看样式指南以获取更多信息.

在您的示例行中:

a = '1' + '2' + '3' + \
    '4' + '5'

或者:

a = ('1' + '2' + '3' +
    '4' + '5')

请注意,样式指南指出,最好使用带括号的隐式连续,但是在这种特殊情况下,仅在表达式周围加上括号可能是错误的方法.

Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.

这篇关于如何在Python中进行换行(换行)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆