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 +<
Your actual code shouldn't work, you are missing whitespaces at the end of "lines" (eg: role.descr as roleFROM...
)
There is triplequotes for multiline string:
string = """line
line2
line3"""
It will contain the line breaks and extra spaces, but for SQL that's not a problem.
tl;dr: Use """\
and """
to wrap the string, as in
string = """\
This is a long string
spanning multiple lines.
"""
From the official python documentation:
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
produces the following output (note that the initial newline is not included):
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
For defining a long string inside a dict, keeping the newlines but omitting the spaces, I ended up defining the string in a constant like this:
LONG_STRING = \
"""
This is a long sting
that contains newlines.
The newlines are important.
"""
my_dict = {
'foo': 'bar',
'string': LONG_STRING
}