What does """ means in python?

S

Sam

For string, one uses "" to represent string. Below is a code fragment that uses """ instead.

cursor.execute("""SELECT name, phone_number
FROM coworkers
WHERE name=%s
AND clue > %s
LIMIT 5""",
(name, clue_threshold))

What does """ means in python?
 
R

Roy Smith

Sam said:
For string, one uses "" to represent string. Below is a code fragment that
uses """ instead.

cursor.execute("""SELECT name, phone_number
FROM coworkers
WHERE name=%s
AND clue > %s
LIMIT 5""",
(name, clue_threshold))

What does """ means in python?

This is what's known as a "triple quoted string" It's just like a
regular string, except that it run across newlines. Very handy for
things like embedding SQL code in a Python program!

It works with single quotes too (i.e. '''this is
a very long string
spread out over several lines'''
 
W

Walter Hurry

Roy said:
This is what's known as a "triple quoted string" It's just like a
regular string, except that it run across newlines. Very handy for
things like embedding SQL code in a Python program!

It works with single quotes too (i.e. '''this is
a very long string
spread out over several lines'''

PMFJI.

When I asked (here) about this a while ago, some kind soul suggested textwrap.dedent.

Any advice as to the pros and cons of the respective approaches (esp. for SQL)?
 
C

Chris Angelico

When I asked (here) about this a while ago, some kind soul suggested textwrap.dedent.

Any advice as to the pros and cons of the respective approaches (esp. for SQL)?

For SQL? Ignore the extra spaces, it's a free-form language. The only
reason to consider dedent() would be if you're worried about how your
log files will look. The actual execution of SQL won't be bothered by
newlines and spaces/tabs.

ChrisA
 
G

Gisle Vanem

 
Gisle V.


"Computers are useless. They can only give answers" --Pablo Picasso
Chris Angelico said:
For SQL? Ignore the extra spaces, it's a free-form language. The only
reason to consider dedent() would be if you're worried about how your
log files will look. The actual execution of SQL won't be bothered by
newlines and spaces/tabs.

Regrading handy uses of ''', you learned me one trick when using Python 
code in a Windows .bat file:


  rem = '''
  @echo off
  echo This is
batch
  \python32\python %0
  echo All done
  exit /b
  rem '''
  import
sys
  print("This is Python")
  for i,p in
enumerate(sys.path):
     print('sys.path[%2d]: %s' % (i, p))
  print("Python
done")

You'll have a variable in Python called 'rem' which contains all
your
batch code :) It exploits the fact that 'rem' makes a
one-line
comment, but the triple quotes go across multiple lines.
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top