Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

3x double quotes vs single quotes, python

Name: Anonymous 2011-01-13 23:27

Quick, easy question:
In Python language:
What is the difference between using either triple `` (double quotes) over using triple ` (single quotes).
What advantages do each provide?

Name: Anonymous 2011-01-14 3:45

>>14
You use single quotes for all regular one-line strings. You use triple quotes for strings that span multiple lines, e.g. embedded SQL commands, docstrings, long output to pass to sys.stdout.write, and so forth. Why is that, you might ask? Because writing
connection.execute("""SELECT *
FROM DB
WHERE A = B
ORDER BY C"""

is a lot more comfy than writing
connection.execute("SELECT *" + \
" FROM DB " + \
" WHERE A = B " + \
" ORDER BY C"


As for your second question, your understanding of %r is flawed. Its only difference to %s is the function that is used to convert the parameter passed. %r uses repr(arg) whereas %s uses str(arg). You can look up these functions in the Python reference[1]. Now, as it happens, repr() uses single quotes to represent that the passed argument was a string, so regardless of what you wrote in your code, repr() will ALWAYS surround the string using single quotes, thus, ' in the string will always end up being escaped.

There is no way to decide what quotes were used in your code after it has been parsed (unless you want to dig into Python internals and function attributes starting with __).


___
[1] - http://docs.python.org/library/functions.html

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List