[Tutor] How to break long lines?

Jim Byrnes jf_byrnes at comcast.net
Fri Feb 22 23:57:49 CET 2013


On 02/22/2013 03:59 PM, Jerry Hill wrote:
> On Fri, Feb 22, 2013 at 4:26 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
>> I am cleaning up my code and have a number of sqlite3 execute statements
>> that extend far past 80 characters.
>>
>>  From my reading implicit line joining with (), [] or {} seems to be the
>> preferred method, but
>>
>> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>>          COLLATE NOCASE', cat)
>>
>> gives this error:
>>
>> jfb at jims1204:~/MyProgs/passwords$ python passwords.py
>>    File "passwords.py", line 50
>>      cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>>                                                                            ^
>> SyntaxError: EOL while scanning string literal
>
> Single quoted strings aren't allowed to have line breaks in them.  If
> you have two string literals separated only by whitespace, though,
> they get joined together, so you could do this:
>
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account'
>          'COLLATE NOCASE', cat)
>
> You can also use triple quoted strings instead, which is my
> preference.  Triple quoted strings are allowed to have line breaks,
> and the whitespace doesn't matter in your SQL query.  So I'd do
> something like this:
>
> cur.execute ('''select account
>                  from pwds
>                  where category = ?
>                  order by account
>                  collate nocase''', cat)
>
> You can break the query up over however many lines it needs to be
> readable, of course.
>

Thanks, the triple quoted method worked great.  I guess I always think 
of them in terms of docstrings or comments not in this way.

Regards,  Jim



More information about the Tutor mailing list