Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python split statement into multiple lines

Tags:

python

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?

like image 330
GJain Avatar asked Dec 16 '22 09:12

GJain


1 Answers

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'.

like image 131
Andrew Clark Avatar answered Dec 29 '22 12:12

Andrew Clark