Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ' ' and " " in python? [duplicate]

Tags:

python

string

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?

like image 957
Tomas Aschan Avatar asked Feb 08 '11 17:02

Tomas Aschan


1 Answers

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).

like image 172
Mark Byers Avatar answered Oct 17 '22 02:10

Mark Byers