如何在代码中打破一个长的字符串字符串并使字符串与其余代码一起缩进? 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
输出错误,但代码看起来更好:
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
答案 0 :(得分:27)
在编译时连接相邻的字符串:
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
答案 1 :(得分:6)
if True:
print "long test long test long test long test long"\
"test long test long test long test long test long test"
答案 2 :(得分:2)
您可以使用尾部反斜杠来连接单独的字符串,如下所示:
if True:
print "long test long test long test long test long " \
"test long test long test long test long test long test"
答案 3 :(得分:0)
为什么没有人推荐三重报价?
print """ blah blah
blah .............."""
答案 4 :(得分:-6)
if True:
print "long test long test long test "+
"long test long test long test "+
"long test long test long test "
等等。