Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string assignment spans multiple lines?

Status
Not open for further replies.

j2ecoder

Programmer
Jan 2, 2006
5
PH
Hello,

I can't have my concatenated string span on multiple lines.
e.g.
str1 = "val1 "
str2 = "val2"
strlong = "line 1" +
str1 +
"line3" + str2

It couldn't also accomblished by using tripple coute """ because the expression would not get evaluated.

(I need to span multiple lines because of assembling long sql query.)

Thanks for any help.
 
What if you put () around the assignment?

strlong = ("line 1" +
str1 +
"line 3" + str2)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
You could also use the % operator to do string substitution:
Code:
string = """blah %s
blah %s blah""" % ( str1, str2 )

String concatenation is so java, %s is much more flexible, readable and pythonic. :)
 
Use \ for line continuation.
Code:
str1 = "val1 "
str2 = "val2"
strlong = "line 1" +\
          str1 +\
          "line3" + str2
print strlong
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top