Python中冗长的单行字符串,不超过最大行长度

JcM*_*aco 1 python string

如何在代码中打破一个长的字符串字符串并使字符串与其余代码一起缩进?PEP 8对此案例没有任何示例.

正确的ouptut但奇怪的缩进:

if True:
    print "long test long test long test long test long \
test long test long test long test long test long test"

>>> long test long test long test long test long test long test long test long test long test long test
Run Code Online (Sandbox Code Playgroud)

输出错误,但在代码中看起来更好:

if True:
    print "long test long test long test long test long \
    test long test long test long test long test long test"

>>> long test long test long test long test long     test long test long test long test long test long test
Run Code Online (Sandbox Code Playgroud)

哇,快答案很多.谢谢!

Noa*_*ing 30

相邻的字符串在编译时连接在一起:

if True:
    print ("this is the first line of a very long string"
           " this is the second line")
Run Code Online (Sandbox Code Playgroud)

输出:

this is the first line of a very long string this is the second line
Run Code Online (Sandbox Code Playgroud)


sun*_*ang 6

if True:
    print "long test long test long test long test long"\
    "test long test long test long test long test long test"
Run Code Online (Sandbox Code Playgroud)

  • 以下是参考:http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation (3认同)