String concat across multiple source code lines

Alex Martelli alex at magenta.com
Wed Aug 2 17:27:55 EDT 2000


"Matt" <mksql at my-deja.com> wrote in message
news:8m9v98$btc$1 at nnrp1.deja.com...
> Is there a better way to do the following?:
>
> sql = "SELECT * FROM TABLE"
> sql = sql + "WHERE TABLE.COL = VAR"
> sql = sql + "GROUP BY COL2"
>
> I would prefer a 'cleaner' way of concatinating a single string across
> multiple program code lines. When using a triple quote, any indentation
> in the code gets included.

Yes, but, semantically, such extra whitespace is innocuous in SQL
(while the missing whitespace between 'TABLE' and 'WHERE' in the
above, and 'VAR' and 'GROUP', would bite:-).

Still, if you just don't want that space, I see that several
suggestions have been advanced.  Explicit + signs are not
needed, since juxtaposed string literals are concatenated (I
hope this is not another case of 'explicit is better'...?-).

You just need to ensure the logical line continues across
several physical lines, and for that purpose I feel an open
parens at the start, and a close on the end, are cleaner than
explicit backslaskes at each line end.

So, summing up, you could have:

sql = (
    "SELECT * FROM MYTABLE"
    " WHERE TABLE.COL = 'VAR'"
    " GROUP BY TABLE.COL2"
)

or whatever format variation strikes your fancy (but do be
sure to include the blanks before the WHERE and GROUP:-).


Alex






More information about the Python-list mailing list