tags:

views:

495

answers:

8

Hello everyone.

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

I'm a begginer 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?

+5  A: 

' 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).

Wahnfrieden
All of the answers were just as good, but this goes for save your wrists. :P
George
Shift counts as a keystroke, whether or not it actually inserts a character itself.
Wahnfrieden
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.
TokenMacGuy
I'll assume this answer was a joke, and just shake my head at it being marked a solution...
Glenn Maynard
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.
Wahnfrieden
On keyboards with a German layout, both ' and " require the use of the shift key. So to wrist saving here.
+1  A: 

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
I prefer consistency of my strings more than being able to avoid escaping characters.
Wahnfrieden
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
@George: None. You are correct.
S.Lott
For the interpreter (compiler) it makes no difference.
gimel
+8  A: 

Consider these strings:

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

Which quote is "best"?

S.Lott
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
I think that his point is: use whichever ensures you won't have to escape what's inside the string.
redtuna
A: 

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
+2  A: 

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
A: 

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.

+1  A: 

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?).

kaizer.se
A: 

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.

Will Harris
Or r'...'
Wahnfrieden