snippetpythonCritical
How do I split the definition of a long string over multiple lines?
Viewed 0 times
howlinessplitovermultiplethelongdefinitionstring
Problem
I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript would be using several sentences and joining them with a
I tried doing something similar in Python, but it didn't work, so I used
Actual code:
See also: How can I do a line break (line continuation) in Python (split up a long line of source code)? when the overall line of code is long but doesn't contain a long string literal.
+ operator (I know, maybe it's not the most efficient way to do it, but I'm not really concerned about performance in this stage, just code readability). Example:var long_string = 'some text not important. just garbage to' +
'illustrate my example';
I tried doing something similar in Python, but it didn't work, so I used
\ to split the long string. However, I'm not sure if this is the only/best/pythonicest way of doing it. It looks awkward.Actual code:
query = 'SELECT action.descr as "action", '\
'role.id as role_id,'\
'role.descr as 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
See also: How can I do a line break (line continuation) in Python (split up a long line of source code)? when the overall line of code is long but doesn't contain a long string literal.
Solution
Are you talking about multi-line strings? Easy, use triple quotes to start and end them.
You can use single quotes too (3 of them of course at start and end) and treat the resulting string
NOTE: Just as with any string, anything between the starting and ending quotes becomes part of the string, so this example has a leading blank (as pointed out by @root45). This string will also contain both blanks and newlines.
I.e.,:
Finally, one can also construct long lines in Python like this:
which will not include any extra blanks or newlines (this is a deliberate example showing what the effect of skipping blanks will result in):
No commas required, simply place the strings to be joined together into a pair of parenthesis and be sure to account for any needed blanks and newlines.
s = """ this is a very
long string if I had the
energy to type more and more ..."""You can use single quotes too (3 of them of course at start and end) and treat the resulting string
s just like any other string.NOTE: Just as with any string, anything between the starting and ending quotes becomes part of the string, so this example has a leading blank (as pointed out by @root45). This string will also contain both blanks and newlines.
I.e.,:
' this is a very\n long string if I had the\n energy to type more and more ...'Finally, one can also construct long lines in Python like this:
s = ("this is a very"
"long string too"
"for sure ..."
)which will not include any extra blanks or newlines (this is a deliberate example showing what the effect of skipping blanks will result in):
'this is a verylong string toofor sure ...'No commas required, simply place the strings to be joined together into a pair of parenthesis and be sure to account for any needed blanks and newlines.
Code Snippets
s = """ this is a very
long string if I had the
energy to type more and more ..."""' this is a very\n long string if I had the\n energy to type more and more ...'s = ("this is a very"
"long string too"
"for sure ..."
)'this is a verylong string toofor sure ...'Context
Stack Overflow Q#10660435, score: 3266
Revisions (0)
No revisions yet.