简体   繁体   English

如何在Python中将表达式拆分为多行

[英]How can I split an expression onto multiple lines in Python

How can I split an assignment like this onto two lines? 如何将这样的作业分为两行?

myLongVariableName = 'some somewhat long format string like this %s' % myOtherVariable

I tried a few variations: 我尝试了一些变体:

myLongVariableName = 'some somewhat long format string like this %s' 
  % myOtherVariable

myLongVariableName = 'some somewhat long format string like this %s' %
  myOtherVariable

myLongVariableName = 
'some somewhat long format string like this %s' % myOtherVariable

My searching is coming up short. 我的搜索即将结束。 What I've found mostly talks about indenting blocks of code or parameters, not long expressions. 我发现的大多数内容都是关于缩进代码或参数块,而不是长表达式。

Adjacent string literals are automatically concatenated in Python: 相邻的字符串文字在Python中自动串联:

myLongVariableName = ('some somewhat long format'
                      'string like this %s') % myOtherVariable

This is an option: 这是一个选择:

myLongVariableName = 'some somewhat long format string like this {}'
    .format(myOtherVariable)

Read more about it: https://docs.python.org/2/library/stdtypes.html#str.format 进一步了解它: https : //docs.python.org/2/library/stdtypes.html#str.format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM