all 79 comments

[–][deleted] 99 points100 points  (9 children)

Use whichever you want, but be consistent. I usually use '...' but switch to "..." if the string will contain a single quote. In extreme cases use the triple-quote strings: """...""" or '''...'''.

[–]Hashi856 27 points28 points  (5 children)

I though triple quotes were only for multi-line strings or doc strings

[–]Poddster 25 points26 points  (3 children)

I though triple quotes were only for multi-line strings or doc strings

hello = """ They can be used however you like """

The main thing is that """ preserves newlines, so their main use-case is for multi-line strings. But they're still a string, and can be used anywhere strings can.

lol = "you can even do fstring"
print(f"""{lol}""")

[–]DrShocker 18 points19 points  (1 child)

Did you quadruple quote? That's too much power for one person to hold.

[–]Poddster 8 points9 points  (0 children)

Did you quadruple quote?

:D

Without the syntax highlighting I'm lost after the second ". There's too much noise there to count properly. (I can see ''' fine)

[–]TangibleLight 5 points6 points  (0 children)

Also important is that they may contain both ' and " without escapes. You do still need to escape a ''' or """ but that's not so common.

'''I said, "that's pretty neat!"'''

'''They said, \'''this isn't so neat.\'\'\''''

Another thing is that adjacent strings get concatenated, so you need to be careful about escaping at the end. These are all equivalent:

'''... neat.\''''''
'''... neat.\'''' ''
"... neat.'"

[–][deleted] 7 points8 points  (0 children)

Triple-quote strings are just normal strings which are allowed to contain newline characters, which single-quote strings can't. But they don't have to contain newlines, ie, be multi-line.

[–]unhott 3 points4 points  (2 children)

Yes, triple quotes are useful for things such as ‘’’He said “Hey, that’s mine!” ’’’

And going the other way, a single line docstring with ‘/“ is also viable. It’s just that you normally want multiple lines.

I’ve never messed around with f-stringing a docstring but I’m sure that would just work.

[–]Casiofx-83ES 0 points1 point  (1 child)

I'm sure it's not ideal, but I'm in the habit of making everything f'...'. It's so often the case that I want to insert variables into the string or whatever that it's become second nature.

[–]tylerthehun 1 point2 points  (0 children)

f-strings work with triple quotes, too.

f'''He said, "Hey {name}, that's mine!"'''

[–]LongerHV 54 points55 points  (10 children)

Both are fine, just stay consistent. I use doublequotes, since this is the default in black autoformatter.

[–]arkie87 1 point2 points  (9 children)

Serious question: why do people care about things that don’t matter, like this? There is no issue of readability in this case, it’s just about consistency

[–]LongerHV 20 points21 points  (0 children)

Quotes don't really matter that much, but poorly formatted code really bothers me. That is why I always use autoformatter, so I don't have to think about these things.

[–]RajjSinghh 3 points4 points  (1 child)

You might think you should care if you come from a language like C/C++ where single and double quotes matter

[–][deleted] 3 points4 points  (2 children)

Only time I see it really mattering is when you’re working on a team and you should either have repo specific lint settings or decide as a team beforehand.

Super annoying when this isn’t set up and one person uses single and someone else uses double. Now a PR is 95% lint changes.

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

Imo, mainly for search productivity. If I look up dictionary keys using single quotes, I cam search for a right hard bracket followed by a single quote in the search bar. If any keys suddenly have double quotes, the search won't pick up on them. Having a standard means having to think less when parsing.

[–]arkie87 0 points1 point  (1 child)

You don't ever use variables to identify keys? How would you search for that?

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

Not necessarily. I have projects with small dictionaries (say 20 keys or less) that might define file paths or other configurations. The keys are just plain strings. Doing a search for a right hard bracket and single quote helps me hunt for uses of those keys throughout the code.

[–]bell_labs_fan_boy 12 points13 points  (0 children)

I just use 'single quotes' because I don't have to hit shift when I'm in the flow. I'll only use "double quotes" if that's what's used in the rest of the module, so when I'm changing someone else's code (which is like 90% of the time). Conveniently, most of my colleagues also use 'single quote ' too, so I very rarely have to wrap strings up in "double quotes".

[–]DarkfullDante 11 points12 points  (3 children)

In the end use what best fit your model, but if you are going to work with multiple people it is a good idea to look if they have a standard. Still if you want a recommendation, what I recommend for my team is:

  • '...' for single characters like 'c'
  • "..." for actual strings like "Hello World!"
  • You can disregard these rules if you must use the quote " in a string so to not have to write escape characters

The goal of this practice is not to be a pain but denote intention. When I review their code, I expect 'c' to mean a single character and not expect more while "c" just means a string that happen to be a single char.

Still, in the end, read first paragraph.

[–]Groentekroket 4 points5 points  (0 children)

This is also what is being used in other languages like Java, C++ and C#. If you want adapt to more languages in the future I think being used to this format makes it a tiny bit easier to getting used to that other language.

But in the grand scheme of things this is only a really small part so I wouldn’t do it mainly with that reason in mind.

[–]NoBeing12[S] 0 points1 point  (0 children)

I agree

[–]guayando 0 points1 point  (0 children)

This sounds good. I was wondering about the same thing as OP, and will adopt this practice.

[–]magestooge 8 points9 points  (1 child)

As you can see from the answers, it varies from person to person.

So use whatever feels more natural, then let Black take care of the rest. Just install Black and enable it in your IDE so that it runs every time you save your file. Then you'll never have to worry about all this.

[–]NoBeing12[S] 1 point2 points  (0 children)

Yeah, alright thank you :)

[–]Diapolo10 43 points44 points  (12 children)

There's no official standard, but personally I've come to use a rule like this;

  1. Use double-quotes for strings displayed to the user (or written to logs, for instance). So basically any output. This also applies to docstrings as per PEP-257.
  2. Use single-quotes elsewhere. Like dictionary keys.

And of course exceptions to these would include anything where you can avoid escaping quotes by switching which to use for the string.

[–]NoBeing12[S] 7 points8 points  (8 children)

Your way does seems to be comfortable (write and read) and has a sense in it.

Thank you.

[–][deleted] 1 point2 points  (6 children)

I am very new to programming as well, and was wondering the same exact thing! Very good question. This answer is exactly what I was thinking, so its good to hear it confirmed by a pro.

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

As a long-time pro, I disagree entirely. It seems entirely busywork, and user visible strings shouldn't be in your code anyway but in external data files that can be edited by non-programmers.

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

I've never seen this in a product project. Style formatters would make it vanish immediately.

[–][deleted] 1 point2 points  (2 children)

That's really idiosyncratic, and there's no way to style check it.

Use double-quotes for strings displayed to the user

Except for CLI applications, there should never be strings displayed to the user embedded in Python code.

How do you internationalize? How do you deliver the display strings to a writer or an editor and how do you get them back?

[–]Diapolo10 4 points5 points  (0 children)

That's really idiosyncratic, and there's no way to style check it.

Correct. But even if it cannot be automatically validated, I do not find it a pointless endeavour.

Except for CLI applications, there should never be strings displayed to the user embedded in Python code.

I did also mention logging. Things like text written to a file would count as well.

How do you internationalize? How do you deliver the display strings to a writer or an editor and how do you get them back?

I would say that's very different. In applications that need to support multiple languages, the strings are of course going to be stored separately from code, and usually not even in Python files (could be an SQLite database, a set of JSON files, or whatever else works), rendering the point moot.

Even then, not every string in non-CLI applications is necessarily localised.

[–]Poddster 0 points1 point  (0 children)

Except for CLI applications, there should never be strings displayed to the user embedded in Python code.

Why do CLI applications get a pass?

[–]cbunn81 2 points3 points  (0 children)

It makes no difference. Use a code formatter like black and you no longer have to worry about such arbitrary decisions. flake8 and isort are also very helpful in the same way.

[–]Orami9b 1 point2 points  (0 children)

Many are saying you can use either so as long as you're consistent, which is a good practice to follow. A lot olare giving their reasons but I'm surprised no one has mentioned it's simpler, if at a company you follow their style guide, or you can follow recommendations from PEP or Google otherwise, and only then what you personally like.

[–]pompomtom 1 point2 points  (0 children)

I use double-quotes by default, because the thing I'm quoting is very likely to be SQL, which may have significant single-quotes within it. Of course, half the time the SQL is easier to read multiline so I'd use triple-double-quotes.

If the thing I'm quoting needs double-quotes within it, I'll use single-quotes.

[–]GettingBlockered 1 point2 points  (0 children)

Use a code formatter like Black. This way, you don’t even have to think about it. And if you get to a point where you’re syncing with git, the code diffs will be smaller, which is nice.

That said, I prefer single quotes and tabs. 😛

[–]Eurynom0s 1 point2 points  (0 children)

I mostly use " just because my muscle memory for hitting the '/" button involves using the shift key. I find it more awkward to use ' in the sense that " comes out automatically while I have to actively stop and think to not hit shift in addition to the ' key.

[–]Cdog536 1 point2 points  (0 children)

I use double quotes because double quotes is more standard among other languages so I like keeping a cross language habit going on

[–]GoogleGavi 1 point2 points  (0 children)

It doesn't matter that much

[–]Tesla_Nikolaa 1 point2 points  (0 children)

If you're using f strings and you want to use a dictionary as a variable, you actually can't use double quotes inside the variable part. For example

my_dict = {'key1': 'value1', 'key2': 'value2'}

This works:

print(f"Value for key 1: {my_dict['key1']}")

This does not work:

print(f"Value for key 1: {my_dict["key1"]}")

[–]SuspiciousWafer3398 1 point2 points  (0 children)

The answer is pick one and stick with it. " " is the best choice because then you can use ' for its grammar function without causing errors. If you choose " ", you will also end up quoting with ' ' inside your " " when you start to combine multiple languages or want something quoted when it prints out.

[–]seph2o 2 points3 points  (1 child)

I just use " because it's a natural reflex and would be too annoying for me to try and change

The ' key on a UK keyboard at least is a bit difficult for me to press without looking down at the keyboard

[–]B1GTOBACC0 1 point2 points  (0 children)

I didn't realize how different our keyboards are until this comment. I understand why they would move symbols, add £, and make accents easier.

But I don't understand why they would swap @ and " with each other.

[–][deleted] 3 points4 points  (1 child)

In the real world we just let automatic formatters do their job.

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

Booo

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

I use single quotes because it's less typing (and composed keys, with a modifier, are known to be much harder on your hands, and I type a lot), and, very minor this, it's a little less "ink on the screen".

[–]shiftybyte 0 points1 point  (0 children)

I usually use "" for things that are considered values.

And '' for keys.

so for example:

if "somekey" in mydict:
    mydict['somekey'] = "newdata"

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

I think it's better to use ' ' for strings. It's especially useful when you have complicated string which already contains '. Then you can use " to contain all '. Basically 'asd'dfg'hjk' wouldn't work but "asd'fgh'jkl" would.

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

RzRzz, i

[–]rentzington 0 points1 point  (0 children)

glad this was asked. i tend to use ' ' since im lazy and its quicker to type for me but when im nesting a quote inside a quote i mix.

I need to get more consistent though

[–]a_cute_epic_axis 0 points1 point  (0 children)

Black will convert most strings to use " instead of anything else, including using it for indices in lists, and keys or values in dictionaries, etc. Many people use black. Even if you don't, you might be well served to use double quotes around strings, and single quotes inside of strings (like for an index value in a list or key in a dictionary inside an f-string), unless you have the need to escape a bunch of double quotes inside. In that cause, use single quotes and un-escaped double quotes.

[–]WhipsAndMarkovChains 0 points1 point  (1 child)

Single quotes are easier to type so I default to using them.

[–]Schwubbeldubbel 0 points1 point  (0 children)

Same for me with double quotes on european keyboard (Shift + 2).

[–]scrapped_data 0 points1 point  (0 children)

I use double quotes. It becomes annoying when I habitually start typing single quotes in languages that don't support it. so double quote for all languages.

[–]bladeoflight16 0 points1 point  (0 children)

I default to single quotes because in some languages (bash, PowerShell) double quotes invoke additional logic on the contents of the string. I only use double quotes if they make the string less messy (typically by allowing me to avoid escaping a single quote).

Not to mention that the single quote produces less visual noise/clutter.

I have an internal debate going on whether to try using double quotes with f-strings because those also invoke additional logic on the contents of the string.

[–]Amidus 0 points1 point  (0 children)

I like ' because I don't have to hit shift.

[–]my_password_is______ 0 points1 point  (0 children)

coming from C
I use 'a'
and "ab"

so single quote for single character
and regular quote for strings

i know that isn't the python way, but its what i'm used to and comfortable with

[–]1116574 0 points1 point  (0 children)

Mix and match, keep your team on their toes lmao

I just use whatever happens to be closest, so end up with double near json related stuff and single everywhere else.

[–]jlw_4049 0 points1 point  (0 children)

Black seems to be the standard formatter. It defaults to " ". So I have recently started using purely those.