Difference between double and single quotes

Thomas Wouters thomas at xs4all.net
Tue Aug 1 06:14:15 EDT 2000


On Tue, Aug 01, 2000 at 09:17:09AM +0000, pauljolly at my-deja.com wrote:

> Please can someone tell me whether Python distinguishes between double
> and single quotes in any way? This was brought to my attention by a
> friend who queried my use of double and single quotes, saying that in
> the UNIX shell a distinction is made.

Yep, in the UNIX shell, and in for instance Perl, double quotes have a
slightly different meaning than single quotes. (For instance, $variables get
expanded.) However, Python doesn't have that subtle difference: double
quotes and single quotes work *exactly* the same, except that you always
need to terminate the quoted string with the *same* quote. If you want to
embed the same quote, you need to escape it, but you needn't escape the
'other' quote. So you can do this:

"I'm a foo"

Or this:

'I\'m a foo'

And the resulting string is exactly the same:

>>> "I'm a foo" == 'I\'m a foo'
1
>>> "I'm a foo" is 'I\'m a foo'
1

The same goes for triple-quoted strings. You can use any kind of quote
inside a triple-quoted string, except the same three-quote-sequence you used
to start the tripled-quoted string:

"""
''' starts a triple quoted string, and so does \"""
"""

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list