Python: what is the "best" literal separator to use?

What is the best literal separator in Python and why? Single "or double"? And most importantly, why?

I'm new to 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 look up the variable" string ". Is this the same case in Python?

+2


source to share


8 answers


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



They are otherwise identical (except that you need to avoid what you choose to use if they appear inside the string).

+9


source


Consider the following lines:

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

      



What's the "best" quote?

+9


source


There are no semantically differences in Python; use either. Python also provides a convenient triple-string separator "" or "that can simplify multi-line quotes. There is also a string literal (r" ... "or r '...') to disallow \ escapes. The language reference has all the details.

+3


source


For string constants containing single quote

, use a separator double quote

.

Another way if you need double quote

inside.

Fast, no-shift input results in single quote

delimiters.

>>> "it very simple"
>>> 'reference to the "book"'

      

+1


source


Single and double quotes work the same in Python. Escapes ( \n

) always work and there is no variable interpolation. (If you do not want to screen, you can use the flag r

as in r"\n"

.)

Since I come from a Perl background, I have a habit of using single quotes for simple strings and double quotes for formats used with an operator %

. But there is no difference.

0


source


Other answers dealt with nested quotes. Another point of view that I came across, but not sure if I am signing, is to use single quotes (') for characters (which are strings, but ord / chr is quick to picky) and use double quotes for strings. What's ambiguous between a string, which must be one character, and one, which is just one character.

Personally, I find that most touch typists are unaffected by the "load" of using the shift key. YMMV from this side. Descending "faster than not using the shift" is a slippery slope. It also makes it faster to use hypercondensed variable / function / class / module names. Everyone just loves the fast and short file names of 8.3 DOS so much. :) Pick what makes semantic sense to you, then optimize.

0


source


This is the rule I've heard about:

") If the string is for the human mind, ie interface text or output, use" "

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

I think that such a well-functioning rule might make sense for a project, but that's nothing I would really care about. I like it above since I read it, but I always use "" (since I knew C was back the first time?).

0


source


I don't think there is one better line separator. I like to use different delimiters to denote different types of strings. In particular, I like to use "..."

to restrict bites that are used for interpolation, or that are natural language messages, and '...'

to delimit small character strings. This gives me a subtle additional clue to the expected usage for a string literal.

I try to always use raw strings ( r"..."

) for regexes because (1) I don't need to hide backslashes and (2) my editor recognizes this convention and highlights the syntax inside the regex.

The stylistic problems of single and double quotation marks are addressed in question 56011 .

0


source







All Articles