13

Possible Duplicates:
Is there any difference between “string” and 'string' in Python?
Single quotes vs. double quotes in Python

I have noted that I can use both single and double quotes to delimit strings in python, and that whichever one I don't use to delimit the string, I can use freely inside it without any need to escape it.

Examples:

ex1 = 'this works'
ex2 = "this works too"
ex3 = "it's much easier to write it's this way"
ex4 = 'but this way, it\'s possible to print out "quotes from other people"'

In other languages, however, I've seen cases both where it doesn't matter (in JavaScript, both 'hi'=="hi" and 'hi'==="hi" return true) and where it does (in C#, "d" is a string while 'd' is a char)

Now, I'm wondering if there's really a difference "under the hood". Does python care which of ' and " I use? If so, in what way?

Community
  • 1
  • 1
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • 2
    I'm sorry if this has been asked before. The search engine won't take ' or ", and my searches for "python string char" and other similar queries returned no clarifying results. – Tomas Aschan Feb 08 '11 at 17:29
  • 3
    possible duplicate of [Is there any difference between "string" and 'string' in Python?](http://stackoverflow.com/questions/143714/is-there-any-difference-between-string-and-string-in-python) (@Thomas: Found it through "python string quotes") –  Feb 08 '11 at 17:30
  • @Tomas: It has, see the link in delnan's comment above. – T.J. Crowder Feb 08 '11 at 17:31
  • @Sven: Not quite, that question is "I know there's no difference, but when should I use which?". –  Feb 08 '11 at 17:31
  • You could use the string `python quotes` in search engine. – Rod Feb 08 '11 at 17:32
  • To really blow your mind check out `"""` and `'''` – chmullig Feb 08 '11 at 17:33
  • @delnan: I just wanted the other link to be in the "possible duplicates" list above since I think it is useful (and the page certainly answers this question). – Sven Marnach Feb 08 '11 at 17:35

1 Answers1

21

There is no difference at runtime. The only difference between the two types of quotes is the one you have already pointed out:

  • Single quotes need to be escaped inside single quoted string literals but not inside double-quoted string literals.
  • Double quotes need to be escaped inside double quoted string literals but not inside single-quoted string literals.

Note: If you use triple-quoted strings ("""foo""" or '''bar''') then you dont need to escape either (unless you happen to have a sequence of three quotes in a row inside the string).

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    So if you need to print a string with quotes you can do 'my "easy" string' and if you need to print a single quote you can do "this ain't hard" - there is no need for any special escape sequence – Martin Beckett Feb 08 '11 at 17:34
  • 1
    6 upvotes and no one bothered to even read the escape sequence part, which is clearly wrong. – user225312 Feb 08 '11 at 17:36
  • I don't see the error.... could you enlighten us? – Mark Byers Feb 08 '11 at 17:58