2

What is the best literal delimiter in Python and why? Single ' or double "? And most important, why?

I'm a beginner in Python and I'm trying to stick with just one. I know that in PHP, for example " is preferred, because PHP does not try to search for the 'string' variable. Is the same case in Python?

David Hall
  • 32,624
  • 10
  • 90
  • 127
George Silva
  • 3,454
  • 10
  • 39
  • 64
  • 1
    Mu; you've asked a question with no answer. Neither is "best". I use double quotes by default because I find them much more natural, but you're asking "which color is better, red or blue?". – Glenn Maynard Aug 21 '09 at 18:38
  • Might want to rephrase the options. Written out literally, **Single ' or double "** become **' or ""** – Travis Aug 21 '09 at 20:18

8 Answers8

9

' because it's one keystroke less than ". Save your wrists!

They're otherwise identical (except you have to escape whichever you choose to use, if they appear inside the string).

aehlke
  • 15,225
  • 5
  • 36
  • 45
  • All of the answers were just as good, but this goes for save your wrists. :P – George Silva Aug 21 '09 at 16:35
  • 1
    Shift counts as a keystroke, whether or not it actually inserts a character itself. – aehlke Aug 21 '09 at 16:53
  • 2
    I like being in the habit of using single quotes. Thats what python emits (usually) when you ask for the `repr()` of a string. The consequence is that if I'm in the habit of using the 's, then doctests end up right the first time. – SingleNegationElimination Aug 21 '09 at 17:35
  • I'll assume this answer was a joke, and just shake my head at it being marked a solution... – Glenn Maynard Aug 21 '09 at 18:40
  • Not a joke. Think how many times a day you type quotation marks - why type twice as much for that? Besides, ' is cleaner looking to me. – aehlke Aug 21 '09 at 20:41
  • 1
    On keyboards with a German layout, both ' and " require the use of the shift key. So to wrist saving here. –  Aug 26 '09 at 07:38
9

Consider these strings:

"Don't do that."
'I said, "okay".'
"""She said, "That won't work"."""

Which quote is "best"?

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • If you are talking about grammar and the ability to output double strings, then single quoting should be better. But thats not always the case. Sometimes you need to output double quotes and single quotes in certain cases (like SQL manipulation), so it depends :P – George Silva Aug 21 '09 at 16:36
  • 5
    I think that his point is: use whichever ensures you won't have to escape what's inside the string. – redtuna Aug 21 '09 at 16:47
  • Doesn't address what the default should be. – aehlke Apr 04 '13 at 21:49
3

Semantically there is no difference in Python; use either. Python also provides the handy triple string delimiter """ or ''' which can simplify multi-line quotes. There is also the raw string literal (r"..." or r'...') to inhibit \ escapes. The Language Reference has all the details.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
1

For string constants containing a single quote use the double quote as delimiter.

The other way around, if you need a double quote inside.

Quick, shiftless typing leads to single quote delimiters.

>>> "it's very simple"
>>> 'reference to the "book"'
gimel
  • 83,368
  • 10
  • 76
  • 104
  • I prefer consistency of my strings more than being able to avoid escaping characters. – aehlke Aug 21 '09 at 16:30
  • you mean a = "Hello this is 'Jason'" ? I know that when i need to use a quote inside a quote, it must be the "other" one. but what are the differences for the interpreter? None? – George Silva Aug 21 '09 at 16:31
  • For the interpreter (compiler) it makes no difference. – gimel Aug 21 '09 at 16:34
0

Single and double quotes act identically in Python. Escapes (\n) always work, and there is no variable interpolation. (If you don't want escapes, you can use the r flag, as in r"\n".)

Since I'm coming from a Perl background, I have a habit of using single quotes for plain strings and double-quotes for formats used with the % operator. But there is really no difference.

Eevee
  • 47,412
  • 11
  • 95
  • 127
0

Other answers are about nested quoting. Another point of view I've come across, but I'm not sure I subscribe to, is to use single-quotes(') for characters (which are strings, but ord/chr are quick picky) and to use double-quotes for strings. Which disambiguates between a string that is supposed to be one character and one that just happens to be one character.

Personally I find most touch typists aren't affected noticably by the "load" of using the shift-key. YMMV on that part. Going down the "it's faster to not use the shift" is a slippery slope. It's also faster to use hyper-condensed variable/function/class/module names. Everyone just so loves the fast and short 8.3 DOS files names too. :) Pick what makes semantic sense to you, then optimize.

user135331
  • 229
  • 1
  • 5
0

This is a rule I have heard about:

") If the string is for human consuption, that is interface text or output, use ""

') If the string is a specifier, like a dictionary key or an option, use ''

I think a well-enforced rule like that can make sense for a project, but it's nothing that I would personally care much about. I like the above, since I read it, but I always use "" (since I learned C first wayy back?).

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
0

I don't think there is a single best string delimiter. I like to use different delimiters to indicate different kinds of string. Specifically, I like to use "..." to delimit stings that are used for interpolation or that are natural language messages, and '...' to delimit small symbol-like strings. This gives me a subtle extra clue to the expected use for the string literal.

I try to always use raw strings (r"...") for regular expressions because (1) I don't have to escape backslash characters and (2) my editor recognises this convention and does syntax highlighting inside the regex.

The stylistic issues of single- vs. double-quotes are covered in question 56011.

Community
  • 1
  • 1
Will Harris
  • 21,597
  • 12
  • 64
  • 64