python将语句拆分为多行

python split statement into multiple lines

我有以下代码:

1
print"my name is [%s], I like [%s] and I ask question on [%s]" % ("xxx","python","stackoverflow")

我想把这条长线分成多行:

1
2
3
4
5
print
 "my name is [%s]"
 ", I like [%s]"
 "and I ask question on [%s]"
  % ("xxx","python","stackoverflow")

您能提供正确的语法吗?


使用隐含的行继续符,将所有内容放在括号内。这是Python风格指南(PEP8)中推荐的方法:

1
2
3
4
print ("my name is [%s]"
      ", I like [%s]"
      "and I ask question on [%s]"
       % ("xxx","python","stackoverflow"))

这是因为python解释器将连接相邻的字符串文本,所以"foo" 'bar'变为'foobar'


另一种风格:

1
2
print"my name is [%s], I like [%s] and I ask question on [%s]" % (
   "xxx","python","stackoverflow")