Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference, if any, between using single quote and double quote in a python dictionary? [duplicate]

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.

like image 584
guagay_wk Avatar asked Aug 10 '18 03:08

guagay_wk


Video Answer


2 Answers

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.

like image 65
user3576467 Avatar answered Oct 08 '22 23:10

user3576467


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)

like image 3
U12-Forward Avatar answered Oct 09 '22 00:10

U12-Forward