Coding Style: Quotes

Skip Montanaro skip at pobox.com
Thu Sep 12 13:59:32 EDT 2002


    John> I'm curious if others have adopted any standards for choice of
    John> single vs.  double quotation marks to delimit strings. A look
    John> through the standard library reveals the choice of one over the
    John> other is rather arbitrary. I suspect it really makes no
    John> difference, but the question has come up during a code review.

In situations where it doesn't matter (one or the other of the quotes
embedded in the string) I sort of half-heartedly pretend I'm programming in
C.  Single-character strings are single-quoted while multi-character strings
are double-quoted.  It really makes no sense to do it this way, and I'm not
very consistent about it.  It's just a carryover from C.  Your code
reviewers might appreciate the consistency if they have a C/C++/Java
background. 

For multi-line strings I use triple quotes.  (Again, the choice of style is
a bit arbitrary.)  Generally I use """ because long chunks of English text
frequently contain contractions.  For long strings which don't contain
newlines (like embedded SQL statements) I tend to enclose multiple "-quoted
literals in parens of some sort, like so:

    rows = self.executesql("select cities.city, state, country"
                           "  from cities, venues, events, addresses"
                           "  where cities.city like %s"
                           "    and events.active = 1"
                           "    and venues.address = addresses.id"
                           "    and addresses.city = cities.id"
                           "    and events.venue = venues.id",
                           (city,))

-- 
Skip Montanaro
skip at pobox.com
consulting: http://manatee.mojam.com/~skip/resume.html




More information about the Python-list mailing list