python split语句分为多行

时间:2013-06-12 22:52:00

标签: python

我有以下代码:

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

我想将此LONG行分成多行:

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

您能提供正确的语法吗?

3 个答案:

答案 0 :(得分:8)

通过将所有内容放在括号内来使用隐含的行继续。这是Python's Style Guide (PEP 8)中建议的方法:

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

这是有效的,因为Python解释器将连接相邻的字符串文字,因此"foo" 'bar'变为'foobar'

答案 1 :(得分:0)

另一种风格:

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

答案 2 :(得分:0)

{'win': 11}
{'win': 211}
{'win': 131}