What's the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?
Solution 1
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.
Solution 2
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. For Unicode docstrings, useu"""Unicode triple-quoted strings"""
.
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.
Solution 3
Choose whatever style you want. Personally I use single quotes everywhere I can in Python.
The documentation states:
"String literals can be enclosed in matching single quotes (') or double quotes (")."
It doesn't matter which one you decide to use. What does matter, is that you stick with your decision. It is good practice to choose a style and stick with it.
Related videos on Youtube
prashu
Updated on June 12, 2022Comments
-
prashu about 2 years
I was just wondering what is the difference between two ways of writing Python Docstrings (
__doc__
):three single quotes:
''' Comment goes here '''
three double quotes:
""" Comment goes here """
Is there any subtle difference in the way doc string could be formatted later while generating docs?
-
Admin over 11 yearsNote: A "doc string" is just a normal string literal as far as the Python syntax/parser cares. The same rules for
"""
,'''
,"
, and'
apply.
-
prashu over 11 yearsThanks @Aesthete. But, I am specially interested in understanding this for doc_strings (doc) for functions and classes.
-
Admin over 11 years@prashu A doc-strring is just .. a string. It's just convention (perhaps stated in a PEP somewhere?) to use the
"""
or'''
quotes, generally to make life easier or more consistent or carry on a tradition .. and it is conceivable that some poorly written code inspection tools require it (but those are broken tools). However, the exact same string literal rules apply - try it! -
Aesthete over 11 yearsA string is a string is a string is a string in Python.
-
Eric Leschinski over 8 yearsYou said: "you can't use the string delimiter inside the string", but I tested it in python3:
foo = """abc"def"""
which succeeds, and foo has the value:'abc"def'
. Also interestingly:foo = """abc"'def"""
is also legal, producing:'abc"\'def'
. -
BrenBarn over 8 years@EricLeschinski: In that case, your string delimiter is
"""
(i.e., three double quotes), not"
.