2

Python's Philosophy:

....
Simple is better than complex. 
....

So why does it use both double quote " " and single quote ' ' to indicate something is a string literal.

I was lost when I typed:

x = "aa"
x

And saw:

'aa'

But not:

"aa"

When I saw the above philosophy, I doubted it. It needs some explanations.

TerryA
  • 58,805
  • 11
  • 114
  • 143
user565739
  • 1,302
  • 4
  • 23
  • 46
  • The most relevant part of the philosophy is "There should be one-- and preferably only one --obvious way to do it.", not "simple vs complex" which is completely unrelated. Anyway, I believe that Guido decided to use both `"` and `'`, since a lot of languages use `'` for characters and `"` for strings, but in python characters a length-one strings, hence people used to denote characters with `'` can do that in python. – Bakuriu Sep 14 '13 at 11:22
  • @Bakuriu *Special cases aren't special enough to break the rules* ;) – TerryA Sep 14 '13 at 14:04

2 Answers2

16

This isn't complex at all. In fact, it's rather helpful.

Both '' and "" can be used in python. There's no difference, it's up to you.

In your example, when you type x, you are given the representation of the string (i.e, it's equivalent to print repr(x)). It wouldn't have mattered if you did x = 'aa'

I like to use either for cases such as:

print 'Bill said, "Hey!"'
print "I'm coming!"

If we used " for the first example, then there would be an error because Python would interpret it as "Bill said, ".

If we used ' for the second example, then there would be an error because Python would interpret it as 'I'

Of course, you could just escape the apostrophes, but beautiful is better than ugly.

Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143
1

You can use "" to be able to write "I'm legend" for example.

to4dy
  • 128
  • 1
  • 1
  • 10