>>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