Skip to main content
 首页 » 编程设计

python之Python 中冗长的单行字符串而不会超过最大行长度

2025年05月04日28EasonJim

如何在我的代码中打断一个长的线性字符串并使该字符串与其余代码保持缩进? PEP 8没有任何关于这种情况的例子。

正确的输出但奇怪的缩进:

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 

输出不佳,但在代码中看起来更好:

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 

哇,很多快速的答案。谢谢!

请您参考如下方法:

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

if True: 
    print ("this is the first line of a very long string" 
           " this is the second line") 

输出:

this is the first line of a very long string this is the second line