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?
There is no difference at runtime. The only difference between the two types of quotes is the one you have already pointed out:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With