What is prefered: 'string' or "string"? (Should be in PEP8 ?)

Erik Max Francis max at alcyone.com
Tue Apr 23 14:20:48 EDT 2002


Petr Prikryl wrote:

> Being quite new to Python -- is there any preference of
> single or double quotes when writing strings?  I am thinking
> about cases when both may be used, not when I need to
> include the alternative inside the string.
> 
> In other words, should I prefer writing:
>     s1 = 'first string'
> or
>     s2 = "second string"
> 
> In yet other words, should I prefer the Pascal look of
> the strings or the C/C++ look of the strings?

It's a style issue, and I'd be highly surprised if there were a 
consensus.  I personally use single quotes for trivial strings (such as 
around invidividual characters, keywords, or identifiers), and double 
quotes for strings that are intended to be English text.  If the text 
spans multiple lines, I use triple double quotes.

> On the other hand, when the Python code is occasionally
> to be converted to C/C++ code, then double quotes
> may be appreciated.

In my opinion, this is a pretty weak reason.  You should never be 
writing code in one language with the hope/intent that someday you're 
going to convert (rather than port) that very code to some _other_ 
language.  Code against the language you're using, and nothing else.

-- 
  Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
\__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ I like young girls.  Their stories are shorter.
\__/ Thomas McGuane
/   Kepler's laws / http://www.alcyone.com/max/physics/kepler/
  A proof of Kepler's laws.




More information about the Python-list mailing list