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

How can I split an expression onto multiple lines in Python

如何将这样的作业分成两行?

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

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

我的搜索即将结束。我发现的主要是关于缩进代码块或参数,而不是长表达式。

相邻的字符串文字自动连接成 Python:

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

这是一个选项:

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

阅读更多相关信息: https://docs.python.org/2/library/stdtypes.html#str.format