4

I have following code:

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

I want to split this LONG line into multiple lines:

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

Can you please provide the right syntax?

GJain
  • 5,025
  • 6
  • 48
  • 82
  • possible duplicate of [Proper indentation for Python multiline strings](http://stackoverflow.com/questions/2504411/proper-indentation-for-python-multiline-strings) – Blorgbeard Jun 12 '13 at 22:54

3 Answers3

11

Use implied line continuation by putting everything within parentheses. This is the method recommended in Python's Style Guide (PEP 8):

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

This works because the Python interpreter will concatenate adjacent string literals, so "foo" 'bar' becomes 'foobar'.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
1

An alternative style:

print "my name is [%s], I like [%s] and I ask question on [%s]" % (
    "xxx", "python", "stackoverflow")
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0
str_val = '''"my name is [%s]"
  ", I like [%s] "
  "and I ask question on [%s]"
  % ("xxx", "python", "stackoverflow")'''

print(str_val)