Hot to split string literals that will across two or more lines ?

Bengt Richter bokr at oz.net
Fri Nov 18 00:27:10 EST 2005


On Fri, 18 Nov 2005 11:33:57 +0800, Xiao Jianfeng <fdu.xiaojf at gmail.com> wrote:

>Lars Kellogg-Stedman wrote:
>
>>>print "a string whcih is very very looooooooooooooooooooooooooooooooooo\
>>>oooooooooooooooooooong."
>>>    
>>>
>>
>>print "a string which is very loooooo" \
>>	+ "ooooong."
>>
Personally, I prefer to use a parenthesized expression format instead of "\" and
(as has been mentioned) to remove the '+' between adjacent string literals,
since the tokenizer will join adjacent string literals into one to generate
a single constant. This is more efficient at run time also, since there are no
byte codes generated for the adding. E.g., the above becomes

  print ("a string which is very loooooo" 
  	 "ooooong.")

or formatted however you please. Sometimes if the substring source code is machine generated,
it is handy to bracket the substrings between a header line and a trailer line, e.g.,

  print (
      "a string which is very loooooo" 
      "ooooong."
  )

Regards,
Bengt Richter



More information about the Python-list mailing list