Hot to split string literals that will across two or more lines ?

S

Steve Holden

Mohammad said:
I tried to use this method in my code like this:-
---------------------------------------------------------------------------------
#!/usr/bin/python


def print_sql():
sql = '''aaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbb'''.replace("\n","")
print sql

print_sql()

---------------------------------------------------------------------------------

the ouput of this is aaaaaaaaaaaaaaaa<space><tab>bbbb......

I can always do this :-
---------------------------------------------------------------------------------
#!/usr/bin/python


def print_sql():
sql = '''aaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbb'''.replace("\n","")
print sql

print_sql()
[...]
In your particular case, if it really is SQL you're dealing with then
you shouldn't worry about what it looks like when you print it - the SQL
interpreter certainly won't care.

Many SQL statements are so long that it actually helps readability to
have newlines in them.

There have been plenty of solutions presented in the earlier posts in
this thread if you really do need to represent multi-line strings.

regards
Steve
 
F

Fredrik Lundh

Mohammad said:
I can always do this :-
---------------------------------------------------------------------------------
#!/usr/bin/python


def print_sql():
sql = '''aaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbb'''.replace("\n","")
print sql

print_sql()

---------------------------------------------------------------------------------

sql = (
'aaaaaaaaaaaaaaaaaaaaaaa'
'bbbbbbbbbbbbbbbbbbbbbbb'
)
print sql

or, perhaps more realistic:

cursor.execute(
'aaaaaaaaaaaaaaaaaaaaaaa'
'bbbbbbbbbbbbbbbbbbbbbbb',
a, b, c
)

e.g.

cursor.execute(
'select * from foo'
' where bar=%s'
' limit 100',
bar
)


</F>
 
M

Magnus Lycka

Fredrik said:
cursor.execute(
'select * from foo'
' where bar=%s'
' limit 100',
bar
)

The disavantage with this is that it's easy to make
a mistake, like this...

cursor.execute(
'select * from foo '
'where bar=%s'
'limit 100',
bar
)

That might be a reason to prefer triple quoting instead:

cursor.execute(
'''select * from foo
where bar=%s
limit 100''',
bar
)

This last version will obviously contain some extra whitespace
in the SQL text, and that could possibly have performance
implications, but in general, I prefer to reduce the risk of
errors (and I've made mistakes with missing spaces in adjacent
string literals).
 
F

Fredrik Lundh

Magnus said:
The disavantage with this is that it's easy to make
a mistake, like this...

cursor.execute(
'select * from foo '
'where bar=%s'
'limit 100',
bar
)

that's why I prefer to put the spaces first. if you do that, you'll spot
the mistakes immediately.

(on the other hand, the chance that the SQL engine won't notice this
typo is pretty slim).
That might be a reason to prefer triple quoting instead:

cursor.execute(
'''select * from foo
where bar=%s
limit 100''',
bar
)

"but it looks ugly"

(as usual, threads like this goes round and round and round ;-)
This last version will obviously contain some extra whitespace
in the SQL text, and that could possibly have performance
implications, but in general, I prefer to reduce the risk of
errors (and I've made mistakes with missing spaces in adjacent
string literals).

absolutely. but if you don't want newlines and whitespace in your
strings, using auto-catenated plain literals is a good alternative, at
least if you combine with a little indentation discipline.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top