python - Pythonic way to create a long multi-line string -


i have long query. split in several lines in python. way in javascript using several sentences , joining them + operator (i know, maybe it's not efficient way it, i'm not concerned performance in stage, code readability). example:

var long_string = 'some text not important. garbage to' +                   'illustrate example'; 

i tried doing similar in python, didn't work, used \ split long string. however, i'm not sure if only/best/pythonicest way of doing it. looks awkward. actual code:

query = 'select action.descr "action", '\     'role.id role_id,'\     'role.descr role'\     'from '\     'public.role_action_def,'\     'public.role,'\     'public.record_def, '\     'public.action'\     'where role.id = role_action_def.role_id and'\     'record_def.id = role_action_def.def_id and'\     'action.id = role_action_def.action_id and'\     'role_action_def.account_id = ' + account_id + ' and'\     'record_def.account_id=' + account_id + ' and'\     'def_id=' + def_id 

are talking multi-line strings? easy, use triple quotes start , end them.

s = """         long string if had         energy type more , more ...""" 

you can use single quotes (3 of them of course @ start , end) , treat resulting string s other string.

note: string, between starting , ending quotes becomes part of string, example has leading blank (as pointed out @root45). string contain both blanks , newlines.

i.e.,:

' very\n        long string if had the\n        energy type more , more ...' 

finally, 1 can construct long lines in python this:

 s = ("this very"       "long string too"       "for sure ..."      ) 

which not include blanks or newlines (this deliberate example showing effect of skipping blanks result in):

'this verylong string toofor sure ...' 

no commas required, place strings joined pair of parenthesis , sure account needed blanks , newlines.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -