faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 19 people (95%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Does Python distinguish between double and single quotes in any way?

Aug 2nd, 2000 03:56
unknown unknown, John Grayson, Aahz Maruch, Erik Max Francis


Single and double quotes may be freely interchanged (so long as
they are balanced %-) ). It is easy, therefore, to construct strings
containing either single or double quotes.
      "It's"       'quote "xxx"'
      lst = ['1', "2", '3']  is equivalent to lst = ["1", '2', "3"]
If you are constructing command strings to use in Unix, then it can
be very convenient. Note that you may still need to escape occasional
quotes...
   look4 = 'file'
   word_count = os.popen("grep 'open(%s \"rb\")' *.py | wc" % look4)
------
In addition to all the other comments, remember that if you have quoting
problems, use """ or ''' (the two triple-quotes) -- you can avoid almost
all quote-quoting that way.
------
They're both interchangeable.  They're used to help with quoting of the
quotes themselves, viz.:
    '"Buk," said the duck.'
    "There's a duck here."
Specifically, in Python the quotes are not used to determine whether
variables are embedded in strings, as they are in shell scripting or
Perl or some other languages; Python does not embed variables that way.