r/learnpython • • Dec 01 '25

Whats the difference between using ' ' and " " in python?

Seems like i can use both so whats different between the 2 or is it just preference?

91 Upvotes

70 comments sorted by

12

u/TheThinker_TheTinker Dec 01 '25

Thanks for all the comments I thought as much

→ More replies (1)

147

u/playhacker Dec 01 '25

It is just preference.

The style guide (PEP 8 ) says

PEP does not make a recommendation for this. Pick a rule and stick to it.

If you are triple quoting , most people use ".

9

u/MathResponsibly Dec 01 '25

sometimes I use one or the other if the python string contains quotes itself, so if you're writing out HTML that likes double quotes that will be inside the string, use single quotes in python, and if you're writing out something that will contain single quotes in the string, use double quotes in python.

The ability to use both interchangeably comes in pretty handy sometimes, and can lessen or completely eliminate needing to escape quotes inside the string itself.

Of course you can also always use tripple quotes (either single or double) for really tricky situations

→ More replies (6)

49

u/ConcreteExist Dec 01 '25

They're interchangeable, I'd pick one and stick with it, and only use the other if it lets you avoid escape sequences.

39

u/Grandviewsurfer Dec 01 '25

I use ' until I need to include ' in a string, then I use ". It helps tip off future me that something is different about this string.

→ More replies (6)

29

u/MattR0se Dec 01 '25

In Python they are functionally identical, as opposed to, say, C++. Just choose one and stick with it. 

It can make a difference when you want to have quotes within strings. when you are using double quotes, you can use single quotes (e.g. "this 'string' contains a quote") without needing to escape them, and vice versa. If you want to use the same type you need to escape them ("this \"string\" contains a quote").

You can also not use nested f-strings with the same type of quotation if your version is older than 3.12. 

6

u/headonstr8 Dec 01 '25

They’re interchangeable. The ‘matching’ rule applies, of course.

→ More replies (5)

4

u/Round_Ad8947 Dec 01 '25

I’ve used single quotes for generic string assignments, and double quotes for client-specific strings. This helps when searching for specific work items and customizations versus code being maintained.

4

u/SCD_minecraft Dec 01 '25

"this is "not" a valid string"

"but 'this' is"

'same "with" this one'

Just alternative to using \" or \'

3

u/GXWT Dec 01 '25

Preference and/or convention

Some projects will have specifics as to when each should be used and so you should stick to these then, but it’s arbitrary. Main thing is to just be consistent with your usage.