Triple quotes delimit a Python string; they do not create comments. A string becomes a docstring only when it is the first statement in a module, class, function, or method. For ordinary explanatory notes that Python ignores, use #.
Why triple quotes are not comments
Python treats a comment and a string as different parts of the language. The Python Language Reference defines a comment as starting with a # that is not inside a string literal and ending at the physical line’s end. Comments are ignored by Python’s syntax.
As an Amazon Associate I earn from qualifying purchases.
Three matching single quotes or three matching double quotes, by contrast, delimit a string literal. Triple-quoted strings can contain unescaped newlines, and those newlines are part of the string. The quotes change how the string is written; they do not turn it into a comment.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
When a triple-quoted string becomes a docstring
Placement is what makes a string a docstring. PEP 257 defines a docstring as a string literal that is the first statement in a module, function, class, or method definition. Python makes that docstring available through the object’s __doc__ attribute.
#1 Best Overall
# Python ignores this comment as syntax.
def parse_record(text):
"""Parse one record and return its fields."""
return text.split(",")
print(parse_record.__doc__)
The function’s docstring sits immediately after its def line, before any other statement. The comment beginning with # is separate and is not exposed as the function’s documentation.
What happens when the string is in the wrong place
A triple-quoted string after another statement is still a string literal, but it is not the function’s runtime docstring. PEP 257 says string literals elsewhere are not assigned to __doc__ or recognized as documentation by the bytecode compiler.
Rank #2
def parse_record(text):
result = text.strip()
"""This does not become parse_record's docstring."""
return result
Here, the string follows an assignment, so it does not document parse_record. To document the function, move the intended docstring directly below the def line. To leave a note for readers of the code, use a hash comment.
Choose the syntax that matches your intent
| Construct | Purpose | Placement and behavior |
|---|---|---|
# explanation |
Commentary for people reading the code | Place it outside a string literal; Python ignores it as syntax and does not attach it to an object’s __doc__. |
"""text""" in an arbitrary position |
A string literal | Can span lines, but does not automatically document an object. |
| A string literal as the first statement in a module, class, function, or method | Documentation for that object | Python assigns it to the object’s __doc__ attribute. |
A string after a simple assignment at module or class level, or in an __init__ method |
An attribute docstring, in PEP 257 terminology | Not assigned to runtime __doc__; some documentation tools may extract it. |
| A string immediately following another docstring | An additional docstring, in PEP 257 terminology | Not assigned to runtime __doc__; some documentation tools may extract it. |
Attribute and additional docstrings are specialized conventions for certain documentation tools. For documentation that should be available on an object through __doc__, use the first-statement position.
Write comments and docstrings clearly
PEP 8 recommends starting each block-comment line with # followed by a space, and keeping comments clear and current. It recommends docstrings for public modules, functions, classes, and methods, with PEP 257 providing further conventions.
PEP 257 recommends triple double quotes for docstrings, including one-line docstrings. A multiline docstring normally begins with a summary line, followed by a blank line and further detail. This is a style convention, not a special meaning assigned to triple quotes by Python’s syntax.
When useful, a function docstring can explain behavior, arguments, return values, side effects, exceptions, or calling restrictions. Avoid adding one that merely repeats what obvious code already says, and keep both comments and documentation accurate as the code changes.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchQuick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




