PDA

View Full Version : small python question



mechel
April 30th, 2008, 05:35 AM
I know this is kind of a small question, but I was wondering in python what the difference is between using ' and using ". Just so you know I'm pretty new to programing and I have searched a bit for the answer, but I haven't been able to find anything.

amingv
April 30th, 2008, 06:10 AM
There's no difference; open an interpreter and observe the following:


>>> "spam"
'spam'
>>> 'spam'
'spam'
>>> a="eggs"
>>> a
'eggs'
>>> if 'bacon' == "bacon":
... print 'They\'re the same'
...
They're the same
>>>

mssever
April 30th, 2008, 06:51 AM
A number of languages distinguish between single and double quotes. Unfortunately, Python is NOT one of them.

A Python quote is roughly equivalent to a double quote in many languages (such as Ruby), while a Python raw string (r' or r") is roughly equivalent to a single quote in those same languages.

ghostdog74
April 30th, 2008, 07:47 AM
There's no difference;
does the below explain any differences?


>>> a="bacon"
>>> a="baco'n"
>>> a
"baco'n"
>>> a="baco"n"
File "<stdin>", line 1
a="baco"n"
^
SyntaxError: invalid syntax
>>> a='baco"n'
>>> a
'baco"n'
>>> a="baco\"n"
>>> a
'baco"n'

samjh
April 30th, 2008, 10:09 AM
No difference.

As a matter of convention, I like to use " " for strings and ' ' for characters (like in C-style languages).

pmasiar
April 30th, 2008, 01:11 PM
A number of languages distinguish between single and double quotes. Unfortunately, Python is NOT one of them.

Why "unfortunately"? It is very convenient if you need one kind of quotes within a string - just use the other one to wrap it. It is **very** convenient to me. :-)

Beyond ', ", r", there is """ quote, which might be multiline, including newlines.

BTW, OP, you should read 'how to ask questions' link in my sig, title of your questions should be '[python] question about quotes' or something. :-)

mechel
April 30th, 2008, 02:46 PM
Thanks for the fast answers guys. I figured there wasn't any difference between the two, but you all just confirmed it for me :).