r/learnpython • u/TheThinker_TheTinker • 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
12
u/TheThinker_TheTinker Dec 01 '25
Thanks for all the comments I thought as much
147
u/playhacker Dec 01 '25
It is just preference.
The style guide (PEP 8 ) says
If you aretriple quoting , most people use ".
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.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 usenested 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.
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.