I declare a python dictionary like this using double quotes;
{
"Name":"John",
"Date":"2/18/1998",
"Profit":25.12
}
The same python dictionary can be written like this using single quotes;
{
'Name':'John',
'Date':'2/18/1998',
'Profit':25.12
}
What is the difference, if any, between the two? What is the best practice in python? Practical-wise, I encounter no difference so far. I'm not sure if I missed out anything.
There's no difference; '
and "
can be used interchangeably. There's no inherent reason to favor one over the other unless your string has a quotation mark in it (in that case, you have to surround the string with the opposite quotation mark).
Other than that, it's just personal preference. If you come from a Java background then you might prefer double quotes for strings and single quotes for "characters" (which are really just one letter strings). But at the end of the day it's the same thing.
The differences is that when you have a string like:
''a''
It will throw a syntax-error because it contains two quotes side by side
so you need to do double quotes like:
"'a'"
And same thing doing the opposite:
""a""
Will throw an error.
'"a"'
won't throw an error
(on stack-overflow the displayed code is already doesn't look right)
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