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
  • 19,274
  • 42
  • 103
  • 174

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
  • 28,423
  • 6
  • 90
  • 122
  • 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
  • 67,464
  • 13
  • 126
  • 132
  • 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,925
  • 13
  • 52
  • 71
1

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

Alvaro
  • 11,797
  • 9
  • 40
  • 57