1

I have been always mixing these two notations, regarding them both as a string in Python.

What are the differences between them?

Under what circumstances can we only use one of them?

Sibbs Gambling
  • 18,128
  • 38
  • 90
  • 168

4 Answers4

2

They're the same. The only time it ever matters is that you have to escape the delimiter character: "\"" vs '"'.

Personally, I usually use ' for strings that aren't "user-visible" and " for strings that are, but I'm not completely consistent with that and I don't think it's common practice.

Danica
  • 27,574
  • 6
  • 88
  • 118
  • I'm really bad at this. I have source files with mixed quotes all over the place... I've recently started working with a linter which complains every time I'm inconsistent in my quoting and it's always complaining at me ... – mgilson Oct 04 '13 at 03:10
2

No difference at all: they mean exactly the same thing. Yes, that's unusual for Python ;-)

Some programmers like to put one-character strings in single quotes, and longer strings in double quotes. Probably a habit carried over from C. Do what you like :-)

Ah: a lot more discussion here.

Community
  • 1
  • 1
Tim Peters
  • 63,230
  • 10
  • 119
  • 128
  • 2
    If you're a fan of programming psychology, here's an oddity I noticed years ago: triple-quoted strings in Python are, at least 10 to 1, spelled with double quotes instead of single quotes. Something about the way our brains are wired ;-) – Tim Peters Oct 04 '13 at 02:39
1

They are equal and depend on your preferences

but you can do this:

>>> print 'Double" quote inside single'
Double" quote inside single
>>> print "Single' quote inside double"
Single' quote inside double
Serial
  • 7,627
  • 13
  • 48
  • 67
1

They are the same, though I prefer to use 'single quotes'as they're easier to read

Alvaro
  • 11,321
  • 8
  • 39
  • 55