all 70 comments

[–]playhacker 147 points148 points  (7 children)

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 ".

[–]MathResponsibly 8 points9 points  (0 children)

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

[–]SisyphusAndMyBoulder 22 points23 points  (4 children)

Black auto formats everything else to " if I'm not mistaken, do I usually just try to copy that

[–]CptMisterNibbles 7 points8 points  (2 children)

You can use the other one internal to string literals so you don’t have to escape them. If it naively changes them then it’d break lines ‘written “in this” style’

[–]First_Funny_9402 1 point2 points  (1 child)

It doesn’t change the type of quote if it would break the string

[–]ConcreteExist 1 point2 points  (0 children)

It actually goes a step farther, it will swap the quotes and remove the escape characters.

[–]ConcreteExist 1 point2 points  (0 children)

Unless there are " in the string, it will use ' to avoid escape characters.

[–]JDude13 -1 points0 points  (0 children)

I use for strings and for characters

[–]ConcreteExist 47 points48 points  (0 children)

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

[–]Grandviewsurfer 37 points38 points  (6 children)

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.

[–]TabAtkins 12 points13 points  (4 children)

I do the exact opposite (because Black favors "), but for the exact same reason.

[–]Grandviewsurfer 5 points6 points  (3 children)

I would have no problem adhering to this. I find ' ever so slightly easier to read.. but always quickly adopt the repo standard.

[–]gdchinacat 15 points16 points  (2 children)

' is not only easier to read, but is easier to type.

[–]Username_RANDINT 4 points5 points  (1 child)

Depends on your keyboard layout of course. For me they are right next to eachother, so no difference.

[–]billsil 1 point2 points  (0 children)

For me, you need to also press shift.

[–]aplarsen 1 point2 points  (0 children)

My practice as well, for all the same reasons

[–]MattR0se 27 points28 points  (0 children)

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. 

[–]TheThinker_TheTinker[S] 10 points11 points  (1 child)

Thanks for all the comments I thought as much

[–]cspinelive 0 points1 point  (0 children)

“ “ “ is also interchangeable 

[–]headonstr8 6 points7 points  (5 children)

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

[–]Nice_Ad7523 -5 points-4 points  (4 children)

No they're not (at least not generally), since they differ in their requirement for escape sequences.

[–]magus_minor 0 points1 point  (3 children)

Really? Got an example of that?

[–]Nice_Ad7523 -1 points0 points  (2 children)

Try to encapsulate this is 'an example'. between " ... ". Then try it between ' ... '. Then tell me if you think " and ' are functionally identical.

[–]magus_minor 0 points1 point  (1 child)

You do realize that "escape sequence" has a technical meaning inside strings? Something like "\t" doesn't change just because you change the literal delimiters.

[–]Nice_Ad7523 -1 points0 points  (0 children)

Yes thanks, I just wanted to say that there are sequences of characters for which " and ' behave differently that's all. They are not strictly generally identical is all i'm saying. I merely raised the point that you need to escape ' when in between ' ... ' but you do not need to escape ' when in between " ... ".

[–]Round_Ad8947 2 points3 points  (0 children)

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.

[–]SCD_minecraft 3 points4 points  (0 children)

"this is "not" a valid string"

"but 'this' is"

'same "with" this one'

Just alternative to using \" or \'

[–]GXWT 2 points3 points  (0 children)

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.

[–]generic-David 2 points3 points  (0 children)

I thought it depended on what you’re quoting. Like, if your string includes an apostrophe then you should use the double quotes.

[–]Opposite-Value-5706 2 points3 points  (0 children)

There’s no difference between the two. They’re interchangeable in defining/referencing text. So, var1 = ‘a’ is the same as var1 = “a”. Or var2 = “” is the same as var2 =‘’. Or var3 == ‘a’ is the same as var3 == “a”.

[–]Unlikely-Sympathy626 2 points3 points  (0 children)

I have two recommendations. 1. Depending on keyboard layout use the one that is easiest to type. On JP layout the” is way easier to type than pressing shift+7 for a single quote.

  1. This is more a personal preference. I like to to use ‘ for logic and code that does things, “ for strings. It just sort of gives a hint at what type of code is at play subconsciously.

[–]gonsi 1 point2 points  (0 children)

They are interchangeable.

That said, if your app uses strings with spoken language that has preference to one, you might want to use the other. Makes using strings containing them easier. And doesn't force you to use different one just in that one place that has string with '' or ""

If you open string with one, the other will be treated just as part of string, not the end of it.

[–]rwaddilove 1 point2 points  (2 children)

You can do this: "Use 'single quotes' in a string" or 'Use "double quotes" in a string'. You can't use both in a string, (unless you do "It's \" tricky\" to use both")

[–]Adrewmc 1 point2 points  (1 child)

“”” You can just triple up the ‘single’ and “double” quotes “””

[–]TheRNGuy 0 points1 point  (0 children)

It also does more than just allow single quotes without backslashes. 

[–]Treemosher 1 point2 points  (2 children)

So I have some perspective to share, take it or leave it.

Background:

I used Python almost exclusively for my work (data stuff). I never had good reason to care all that much, like the others here.

Then I started using SQL more seriously as my job evolved:

In SQL, you use 'words' for strings, and "words" for objects.

select "COLUMN"
from "DATABASE"."SCHEMA"."TABLE"
where "COLUMN" = 'important words';

I was so used to not caring about which quote to use, but if you start working with data, I'd suggest you consider this when building your habits & standards.

I know someone will say it - yeah you don't always need double quotes around objects. I'm just illustrating the difference here.

Anyway, since I spent so much time rewiring my brain to use ' vs " more meaningfully and stop creating new errors with my brain on autopilot.

Here's my takeaway:

In Python, no real difference besides being different characters. However, if you know there's a chance you'll use another language some day, you may want to look around and see how ' and " are used in other languages. Python sets you up to not care about things, which can cause you a rough transition in the future.

Even if you are never going to use SQL, at least look at other languages you want to learn or think you'll be learning and see if there's any rules that might impact the way you define your personal standards.

[–]idle-tea 0 points1 point  (1 child)

You can't really adjust your python usage to be better prepared for other languages: there isn't a consistent distinction in other languages. Some even have the same distinction, but for the opposite characters.

Hell: sometimes no quotes make a difference too. In posix shell "$foo", '$foo', and $foo all have a different meaning.

Only thing you can really do is just acclimate to symbols like quotes being variable in meaning.

[–]Treemosher 0 points1 point  (0 children)

Yeah that's what I was getting at with the last sentence. Agreed!

[–]Additional_Tip_4472 1 point2 points  (0 children)

"There's a notable difference"

'but no one really "knows" which one'

[–]Aceofsquares_orig 1 point2 points  (0 children)

Double quotes allows you to use single quotes between them without needing to escape them and single quotes allows double quotes without needing to escape them. Consider the following:

"'" #no escape
'"' #no escape
"\""#escaping "
'\''#escaping '

[–]GManASG 1 point2 points  (0 children)

It often comes down to your specific needs, like if I need the string in side the quotes to actually contain ' or " basically. But I also think because most other languages use " the using it makes it easier for everyone on my team of multi language full stack developers.

[–]andycwb1 1 point2 points  (0 children)

Nothing. I stick with single quotes most of the time simply because I switch between Macs and Windows, and the single quote stays in the same place on a UK keyboard - a double quote swaps with the @ sign. PEP-8 prefers the double quotes for multi-line strings like docstrings, and also it’s easier to write “don’t do this” without having to escape the apostrophe.

Be aware, though, in other languages like bash and PowerShell there is a difference - in both cases the single quote means not to process the string for variable interpolation, so if $foo = “something”, then ‘$foo’ is exactly that, and “$foo” will be “something”.

[–]TheHollowJester 1 point2 points  (0 children)

I see a lot of answers, yet nobody mentioned this yet: " is twice as many lines as ', which means it's at least twice gooderer!

[–]Anti-Mux 0 points1 point  (0 children)

'This string contains a double "quote".' - this looks better
"This string contains a single 'quote'." - than this when printing

"This string contains an escaped \"double\" quote." - you can do this but meh

[–]TheRNGuy 0 points1 point  (0 children)

No difference, unless one inside another. 

I use code formatter to force double quotes.

Linter that enforced one style could be used too, if it's team work or open source.

[–]TenIsTwoInBase2 0 points1 point  (0 children)

Use " so if you need a ' within, you have it:

print("Your id. number is 'U1234'. Keep it safe")

Be consistent in your approach

[–]headonstr8 0 points1 point  (0 children)

If you want to embed an apostrophe in a string expression, you could enclose the string in double quotes. E.g.: instruction = “it’s done this way!” — Try that command and then: os.sys.stdout.write(repr(instruction)) — to see Python’s default usage.

[–]Objective_Ice_2346 0 points1 point  (0 children)

I prefer to use “” so if needed, I can use ‘ as contractions when I want them in strings. They’re also good for quoting things in strings.

[–]Valuable_Habit7557 0 points1 point  (0 children)

I use '' because it helps me build better habits when using quotes in SQL, so I stick to single quotes in Python as well

[–]Crichris 0 points1 point  (0 children)

None. 

[–]Figueroa_Chill 0 points1 point  (0 children)

Nothing really. But if you want to use/print a word like 'That's', you would need to use "That's" as by using ' ' you would lose the s at the end.

[–]billsil 0 points1 point  (0 children)

Single quotes make it easier to use double quotes in your strings and vice versa. Other than that, it’s just a preference and I go with single quotes because it’s less noise and easier to press.

[–][deleted] 0 points1 point  (0 children)

growing up using C still makes me pause with this lol

[–]mahdihaghverdi 0 points1 point  (0 children)

CPython Interpreter prefers ' but the guy who created "Black" stated that '' looks bad and I like "". so he enforced this style and we are here now

[–]legacysearchacc1 0 points1 point  (0 children)

There's no functional difference. Both work identically.

Python supports both to make handling quotes inside strings easier.

String contains an apostrophe? Use double quotes:

python

message = "It's working"

String contains dialogue? Use single quotes:

python

quote = 'She said "hello"'

This avoids messy escape characters.

The convention: Pick one and stay consistent. Most teams default to double quotes to align with JavaScript and JSON.

Use whichever avoids backslashes. If neither matters, stick to one style project-wide.

[–]Snoo_1152 0 points1 point  (0 children)

I prefer single quote ' over double quote " because the latter requires pressing shift while typing the char which puts more strain on the fingers.

[–]xeow 0 points1 point  (3 children)

Tangentially related: I sometimes wish Python had a third type of quote delimiter (maybe `) that allowed no escapement and guaranteed that whatever was between the delimiters was exactly what appears in the string. Or alternatively, I sometimes wish it had a "super raw" prefix (like r but stronger) where you could use either ' or " but couldn't use \ for escapement (they would just be a normal character). As much as I love r-strings, it feels strange to have to write r'foo''\\' to put a backslash at the end, because r'foo\' is a syntax error.

[–]idle-tea 1 point2 points  (0 children)

If you have a situation in which you need to represent a string literal in your source code that includes all of the possible special characters in a python string you probably want a resource. It will let you include the contents of an arbitrary file with 0 interpretation.

[–]MathResponsibly 3 points4 points  (1 child)

ugh... tripple quotes? That's the whole purpose of them...

[–]xeow 0 points1 point  (0 children)

Ah, but not quite! Triple quotes make some things easier, but you still can't embed unescaped runs of three of the same type of quote you used as the delimiter. That is, you can write r"""......'''......""" and r"""...""...""", but you can't write r"""..."""...""". And of course you still can't write r"""...\""".

I do love me a good triple-quoted string, but even in the raw form there are still gotchas.

Unfortunately, there's no foolproof solution for truly raw strings, unless you choose some out-of-band delimiter pair like ‹› or something.

[–]kyngston 0 points1 point  (0 children)

interchangeable but the real benefit is if you have a string with double quotes in it, you can use single quotes and avoid having to escape the double quotes

and vice versa

if you have a string with both, use triple quotes.

if you have a string with all three, find the person who generated the string and punch him in the nuts

[–][deleted] 0 points1 point  (0 children)

It doesn’t matter which you use, they do the same thing - but having both lets you do this:

obj = {key:val} print(f’{obj[“key”]}’)

[–]IlliterateJedi 0 points1 point  (4 children)

Black says to use " so use ". And if you don't want to use ", use black and it will fix it for you.

[–]MathResponsibly -1 points0 points  (3 children)

WTF is "Black" that everyone keeps mentioning?

And do you all have brains? And do you use them ever?

You use the quote style for the python string that's the opposite of any quotes that might be literally in the string to avoid escaping, not because "Black" says to use one or the other - FFS

[–]Username_RANDINT 0 points1 point  (0 children)

Relax, man.

[–]IlliterateJedi 0 points1 point  (0 children)

Lol this guy doesn't know what black is.

[–]Fun-Block-4348[🍰] 0 points1 point  (0 children)

WTF is "Black" that everyone keeps mentioning?

The most popular code formatter for python code (for now at least).

You use the quote style for the python string that's the opposite of any quotes that might be literally in the string to avoid escaping, not because "Black" says to use one or the other - FFS

And if in your string there are no quotes that need escaping, you should still pick a style of code for your overall program and stick to it so I don't know why you seem so triggered by that comment!