2060

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'; 

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.

5
  • 355
    Since your example looks like a SQL block just waiting for an injection attack, another suggestion is to look into a higher level SQL library like SQLAlchemy or something to steer clear of hacking together raw SQL like this. (Perhaps off-topic, but you did ask for "Any suggestions". ;) Commented May 18, 2012 at 22:26
  • 11
    This is "Pythonic way to create multi-line code for a long string" To create a string containing newlines see textwrap.dedent. Commented Jan 29, 2015 at 17:16
  • I wrote a small package for this. Example here: stackoverflow.com/a/56940938/1842491 Commented Jan 24, 2020 at 14:49
  • Related: How to write very long string that conforms with PEP8 and prevent E501 Commented Feb 24, 2022 at 18:12
  • @JohnGainesJr. Would the SQL chunk above be resistant to SQL injection attacks if the account_id and def_id were sanitised? I am using Pydantic via FastAPI to ensure user input passes a regex pattern, but if this is not safe it would be good to know. I am not anti-SQLAlchemy, just exploring the space. Commented Aug 16, 2023 at 8:33

31 Answers 31

1
2
-12

Generally, I use list and join for multi-line comments/string.

lines = list() lines.append('SELECT action.enter code here descr as "action", ') lines.append('role.id as role_id,') lines.append('role.descr as role') lines.append('FROM ') lines.append('public.role_action_def,') lines.append('public.role,') lines.append('public.record_def, ') lines.append('public.action') query = " ".join(lines) 

You can use any string to join all these list elements, like '\n'(newline) or ','(comma) or ' '(space).

Sign up to request clarification or add additional context in comments.

3 Comments

Why wouldn't you at least use an array literal?
Arrays are represented by class list. check out another discussion regarding the array literals
I suppose this works, but you should think about performance and readability...
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.