The informational document PEP 257 -- Docstring Conventions recommends to use """triple double-quotes""" for consistency, and all their examples show the same:
For consistency, always use
"""triple double quotes"""around docstrings. User"""raw triple double quotes"""if you use any backslashes in your docstrings.
Whether to use single quotes or double quotes is only a stylistic issue in practice. There will be no difference in formatting when generating and publishing docs from these strings.
Note that code formatters such as black will enforce triple double-quotes for docstrings.
Answer from wim on Stack OverflowThe informational document PEP 257 -- Docstring Conventions recommends to use """triple double-quotes""" for consistency, and all their examples show the same:
For consistency, always use
"""triple double quotes"""around docstrings. User"""raw triple double quotes"""if you use any backslashes in your docstrings.
Whether to use single quotes or double quotes is only a stylistic issue in practice. There will be no difference in formatting when generating and publishing docs from these strings.
Note that code formatters such as black will enforce triple double-quotes for docstrings.
No. They are the same. The only difference is that the first one can contain a sequence of three unescaped double quotes, while the second can contain a sequence of three unescaped single quotes. (In other words, because the delimiters are different, there is a slight difference in what characters you can use inside them.)
Docstrings are just regular strings, and in Python there is no difference between the different string delimiters, except that, of course, you can't use the string delimiter inside the string.
Is there a "rule of thumb" to decide whether to use single quotation mark or double quotation mark? For example, I've seen some people use single quotation mark for small symbol-like strings and double single quotation mark for interpolation or that are natural language messages.
Or is it simply a personal preference and being consistent with whatever style you have for yourself? Obviously, the offical docs state that we can use whichever matching quotes, but I'd like to know if there is a pythonic way to use them.
Formatter: Docstrings with `quote-style = "single"` configuration
coding style - Single quotes vs. double quotes in Python - Stack Overflow
Triple double-quotes used as a docstring or multi-line string?
Which is more popular to use to quote strings, single quote('string') or double quote("string")?
I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren't needed.
For example:
LIGHT_MESSAGES = {
'English': "There are %(number_of_lights)s lights.",
'Pirate': "Arr! Thar be %(number_of_lights)s lights."
}
def lights_message(language, number_of_lights):
"""Return a language-appropriate string reporting the light count."""
return LIGHT_MESSAGES[language] % locals()
def is_pirate(message):
"""Return True if the given message sounds piratical."""
return re.search(r"(?i)(arr|avast|yohoho)!", message) is not None
Quoting the official docs at https://docs.python.org/2.0/ref/strings.html:
In plain English: String literals can be enclosed in matching single quotes (') or double quotes (").
So there is no difference. Instead, people will tell you to choose whichever style that matches the context, and to be consistent. And I would agree - adding that it is pointless to try to come up with "conventions" for this sort of thing because you'll only end up confusing any newcomers.
"A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the _doc_ special attribute of that object."
I thought triple double quotes were only used as docstrings but I came across a SQL command using triple quote for multi line purposes and avoid escaping, I think
rows = db.execute("""SELECT * FROM places
WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude AND longitude <= :ne_lng)
GROUP BY country_code, place_name, admin_code1
ORDER BY RANDOM()
LIMIT 10""",
sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)In this case, triple quotes are just treated as a string literal but not a doc special attribute. How's one treated like a docstring and not the other? Then is this considered as a simple string literal not docstring?:
def foo():
somecode """docstring"""I have recently started learning python and I dont want to make a habit of quoting in one style and later finding out that there is more efficient way of quoting and I have to change my habit. Like with making variable names with lower case alphabets and using underscore instead of spaces.. So, is there a standard for quoting or if not, which type of quoting is generally done in professional or while working in open source projects.
PEP-8 is quite flexible about how to quote strings:
In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability.
For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.
Styles observed in the wild:
Excluding docstrings, (as PEP-257 clearly states "always use """triple double quotes""""), which do you prefer?
-
Single quotes always.
-
Double quotes always.
-
Single quotes unless the quoted string includes apostrophes.
-
Double quotes unless the quoted string includes double quotes.
-
Double quotes for user-facing string, and single quotes for other (code)
strvalues. -
Double quotes for multi-character strings, single quote for single character.
-
Other (please specify).