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

python string line break / python / string / multiline / multilinestring

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 + 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';
query = 'SELECT action.descr as "action", '\
    ' as role_id,'\
    'role.descr as role'\
    'FROM '\
    'public.record_def, '\
    'WHERE = role_action_def.role_id AND'\
    ' = role_action_def.def_id AND'\
    ' = 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


Answer #1

I find that when building long strings, you are usually doing something like building an SQL query, in which case this is best:

query = ' '.join((  # note double parens, join() takes an iterable
    "SELECT foo",
    "FROM bar",
    "WHERE baz",

What Levon suggested is good, but might be vulnerable to mistakes:

query = (
    "SELECT foo"
    "FROM bar"
    "WHERE baz"

query == "SELECT fooFROM barWHERE baz"  # probably not what you want