19

Is there any difference between the use of double quotes to single quotes in Python?

"A string with double quotes"
'A string with single quotes'

Are they identical? Are there differences in how python interprets these strings?

Jerub
  • 41,746
  • 15
  • 73
  • 90
  • Not exactly the same question, but the answer to both is in that thread. – i_am_jorf Mar 10 '09 at 01:56
  • 2
    This is not a duplicate of that question, whatsoever. The fact that the answer to this question is incorporated in that question does **NOT** make it a dupe. Please nominate this question for reopening and join me in the fight against overzealous moderation. – Neil Traft Jan 26 '14 at 06:24

3 Answers3

26

Short answer: almost no difference except stylistically.
Short blurb: If you don't want to escape the quote characters inside your string, use the other type. eg:

string1 = "He turned to me and said, \"Hello there\""

would be slightly more unsightly than saying

string2 = 'He turned to me and said, "Hello there"'

The same applies to single quotes/apostrophes.

Andrew Szeto
  • 1,199
  • 1
  • 9
  • 13
4

Not generally, you just have to be consistent within a given statement. E.g., don't do "foo'

For further reading, see here.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
1

Double quotes look distinctive. Single quotes look like single ticks. Apart from that ...

Rook
  • 60,248
  • 49
  • 165
  • 242