Difference between " and '

François Pinard pinard at iro.umontreal.ca
Fri Jul 22 09:05:14 EDT 2005


[b83503104 at yahoo.com]

> Can someone tell me the difference between single quote and double
> quote?

There is no strong reason to use one and avoid the other.  Yet, while
representing strings, Python itself has a _preference_ for single
quotes.  Programmers can put this duality to good use, by adopting
reasonable conventions, which are nothing more than conventions! :-)

I would guess many people choose one over the other for minimizing the
amount of backslash escape needed.  Some people keep double quotes for
strings that would later undergo formatting, maybe because in most
shells and some other languages, double quotes allow for substitution
and single quotes prevent it.  But in my opinion, while not dismissing
a lot of wisdom developed within other languages, it may be foolish
letting other languages blindly dictate what is best Python style.

Personally, I keep single quotes for "computer" strings, and double
quotes for "human" strings.  To segregate between "computer" and "human"
character for a string, I merely ask myself: "If I wanted to use this
program in another language, would I want this particular string
translated or not?".  Yes means double quotes, no means single quotes.
As single quotes are often use within human text, as apostrophes, this
was a wise choice as far as shell escaping goes.

This is extendable to triple-quoted strings.  I use triple-double quotes
for doc strings meant to hold documentation, which is the usual case.
If doc strings are used for BNF snippets, like with SPARK, or any other
machine data, triple-single quotes are mandatory by my own convention.

-- 
François Pinard   http://pinard.progiciels-bpi.ca



More information about the Python-list mailing list