2

Possible Duplicate:
Single quotes vs. double quotes in Python

Usually we represent , create strings like this

 s = 'abc'

and also like this

 c = "abc"

Ideally both ways are same , than why we have two syntax to do the same ? Is there any difference between these two or Ideally they are same.

Community
  • 1
  • 1
NIlesh Sharma
  • 5,445
  • 6
  • 36
  • 53

1 Answers1

9

The are essentially the same, except for what you have to escape:

"'"
'"'

both work, but to incorporate multiple quote types you must escape the ones used to create the string:

"\"'"
'"\''

The two exist to make it easy for you to avoid having to escape your quotes, so the following two are easy:

'She said: "Not so fast!"'
"Won't you come with us?"

Note that there are also tripple-quote variants:

"""Now I can use either quote with more freedom: ' and "."""
'''Now I can use either quote with more freedom: ' and ".'''

These also allow newlines to be included without escaping:

"""A
multiline
string
is
easy.
"""

That last example would require you to use excessive \n escape sequences otherwise.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    The question will probably be closed as a duplicate. But I consider your answer better than the ones existing on that question. Could you maybe copy your answer to that question? – Mizipzor Sep 04 '12 at 10:42
  • @mizipzor: Note that closing is not the same as deletion; my answer will still be available. You can flag the question for merging if you feel strongly about it, that'll preserve my answer with votes too. Use the `Other` option and explain your reasoning. – Martijn Pieters Sep 04 '12 at 10:43