Syntax - long string/line contiuation in Python script

I’m still fairly new to Python and the syntax. If I have a fairly long SQL statement string I need to build (don’t want to put it in named query yet), what is the syntax for building a long string on multiple lines (continuation)? below is the example. Thanks.

strQuery = 'SELECT DISTINCT WORKORDER.SONum, WORKORDER.SOItemNum \WORKORDER.Lot, WORKORDER.Status, WORKORDER.DieNum, WORKORDER.BilLen, WORKORDER.LotLen, WORKORDER.LotPc, WORKORDER.LotWt, WORKORDER.Press,
WORKORDER.ExtLen, WORKORDER.SchedDt, WORKORDER.ExtrudedPc, WORKORDER.SchedSeq,… (and so on)

3 times "(double quotes)
be wary of the indents tho its one of the few times there shouldnt be any.
im not sure how good this will work for your query tho with the newlines(enters) in it
image

1 Like

Yes, that’s it. Thank you.

1 Like

In your case, I wouldn’t use triple quotes. The problem with triple quotes is that everything between the 2 sets of quotes is part of the string. That includes newlines.

x = """foo
bar
baz"""
# x == "foo\nbar\nbaz"

There is a way in python to ignore newlines, and that’s parenthesis.

x = (
  "foo"
  "bar"
  "baz"
)
# x == "foobarbaz"

It’s up to you to decide which one fits your needs, but I believe in this case what you want is the second method.

2 Likes

I think you've missed the closing parenthesis.

@jeff.ryan - Tip: use the </> code formatting button to format your code. This will preserve indentation and apply syntax highlighting. There's an edit button (pencil icon) below your question should you want to fix it.

strQuery = ('SELECT DISTINCT 
          WORKORDER.SONum,  WORKORDER.SOItemNum,
          WORKORDER.Lot,    WORKORDER.Status,  WORKORDER.DieNum, WORKORDER.BilLen, 
          WORKORDER.LotLen, WORKORDER.LotPc,   WORKORDER.LotWt,  WORKORDER.Press,
          WORKORDER.ExtLen, WORKORDER.SchedDt, WORKORDER.ExtrudedPc,
          WORKORDER.SchedSeq,… (and so on)'
)

(Note that the syntax highlighting doesn't work in this case as the query is a string rather than plain SQL.)

Indeed I did, I also forgot to put some indentation to show that this method doesn’t care about indentation either. Fixed.

Ah yes i tried to test the () too i might have made a typo xD
i like to put a + after each string tho, to know it concats even if its not needed
image

Thanks all. Although the original triple quotes solution got me off and running and worked, this additional syntax looks better to use and good to know.

The whole point of the parentheses is to avoid concatenation !
Another way to signal your line is continuing on the next one in python, though I don't like it, is \.

x = "foo"\
    "bar"
# x = "foobar"

It works because SQL doesn't care about whitespaces either, you just need to know about the differences between the two syntaxes so you can pick the one that matches your need.

Hmm not quite. Python just does auto concats of strings, regardless of parentheses, but i find that very confusing so i always do a + in between xD Maybe a habbit from a different script langague
image

Yes I didn’t express myself clearly, I need to take a few steps back to explain what I meant:

  1. I don’t like the + concatenation. I believe this shouldn’t be used with strings.
  2. if concatenating strings with either the concat method or formatting methods (most of the time, string concatenation is done to build a string from variables, which I believe should be done with formatting methods instead) then you’re already working inside parentheses where whitespace won’t matter
  3. There is little to no point in concatenating literals unless you’re working on several lines, where the parentheses or \ can be used. Hence, the whole point of using parentheses for me in the case at hand (splitting a literal into several smaller ones) is to avoid doing the concatenation yourself.
1 Like

It is specified in the language. Might as well command the tides to cease.

All to avoid using multi-line string literals (triple-quoted). Also specified in the language. Anything else looks un-pythonic (ugly). I recommend working with the language, not fighting it.

3 Likes

But triple quote literals don’t do the same thing. If you want to ‘line wrap’ your literals because they’re too long, triple quotes are not what you’re looking for.

You’re talking about SQL. Line endings are legitimate whitespace. All the online resources I know that show complex SQL show it formatted in multiple lines. Some purists even put every SELECT item on its own line. Python’s multi-line literals are perfect for well-formatted SQL.

3 Likes

Ha I have started doing that, at least in my named queries. Easier to only scroll vertically than scroll vertically and horizontally just to read the query.

I’m not talking about SQL, I’m talking about literals in general.
While in this specific case it is SQL, the author also specified “building a long string on multiple lines (continuation)”. Triple quotes literals will not ‘continue’ the line.

Also, frankly, there aren’t many things in python uglier than triple quotes messing up your indentation.