# Syntax - long string/line contiuation in Python script

**URL:** <https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711>\
**Category:** Ignition\
**Tags:** vision, scripting, ignition81\
**Created:** [March 28, 2022, 12:42pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711 "2022-03-28T12:42:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff.ryan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jeff.ryan/32/25543_2.png) [@jeff.ryan](https://forum.inductiveautomation.com/u/jeff.ryan)\
**Post date:** [March 28, 2022, 12:42pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/1 "2022-03-28T12:42:33Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [March 28, 2022, 12:50pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/2 "2022-03-28T12:50:28Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0fbcafe5621f366f6f05acbd213af5b6168f0c3f.png)

---

<div class="post-metadata">

**Author:** ![jeff.ryan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jeff.ryan/32/25543_2.png) [@jeff.ryan](https://forum.inductiveautomation.com/u/jeff.ryan)\
**Post date:** [March 28, 2022, 1:40pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/3 "2022-03-28T13:40:42Z")

</div>

Yes, that’s it. Thank you.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 28, 2022, 9:27pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/4 "2022-03-28T21:27:09Z")

</div>

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.

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

```

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

```python
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.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 28, 2022, 9:37pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/5 "2022-03-28T21:37:58Z")

</div>

> [@pascal.fragnoud](#):
>
> ... and that’s parenthesis.

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.

```SQL
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.)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 28, 2022, 9:40pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/6 "2022-03-28T21:40:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [March 29, 2022, 11:10am UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/7 "2022-03-29T11:10:14Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/2/424ec5b16287651dfdbc40f4e32c93617ed95f95.png)

---

<div class="post-metadata">

**Author:** ![jeff.ryan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jeff.ryan/32/25543_2.png) [@jeff.ryan](https://forum.inductiveautomation.com/u/jeff.ryan)\
**Post date:** [March 29, 2022, 12:49pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/8 "2022-03-29T12:49:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 31, 2022, 7:44am UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/9 "2022-03-31T07:44:51Z")

</div>

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 `\`.

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

```

> [@jeff.ryan](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [March 31, 2022, 9:30am UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/10 "2022-03-31T09:30:15Z")

</div>

> [@pascal.fragnoud](#):
>
> the whole point of the parentheses is to avoid concatenation

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/6/b6b926343ad2b0478866ec5d28f574e29111fe9f.png)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 31, 2022, 9:40am UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/11 "2022-03-31T09:40:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 31, 2022, 11:29am UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/12 "2022-03-31T11:29:46Z")

</div>

> [@pascal.fragnoud](#):
>
> I don’t like the `+` concatenation. I believe this shouldn’t be used with strings.

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.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 31, 2022, 11:56am UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/13 "2022-03-31T11:56:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 31, 2022, 12:33pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/14 "2022-03-31T12:33:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 31, 2022, 12:53pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/15 "2022-03-31T12:53:07Z")

</div>

> [@pturmel](#):
>
> Some purists even put every SELECT item on its own line

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.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 31, 2022, 1:27pm UTC](https://forum.inductiveautomation.com/t/syntax-long-string-line-contiuation-in-python-script/57711/16 "2022-03-31T13:27:18Z")

</div>

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.
