Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

docstring — documentation string

A docstring is an ordinary string literal (not an f-string or a t-string) used as the first statement of a module, class, or function. It documents the object's purpose and is available through __doc__; it is not a comment beginning with #.

Version: Python 3.14; repository image, production runtime not attested

Syntax

def function_name():
    """Describe the function's purpose."""
    pass

A docstring is an ordinary string literal (not an f-string or a t-string) used as the first statement of a module, class, or function, including a method. Python exposes it as the object's __doc__ attribute. Tools can use this content to present documentation.

Neither a formatted string literal (f-string) nor a template string literal (t-string) creates a docstring, even if it has no replacement fields and is the first statement. If a function's first statement is f"""Description.""", its __doc__ is None.

A comment beginning with # does not create a docstring. A standalone string literal placed after another statement does not become the object's main docstring either. Triple quotes """...""" are convenient, especially for multiple lines, but are not enough on their own: both the kind of literal and its position matter.

By convention, the first line should briefly describe the object's purpose, start with a capital letter, and end with a period. Separate further description with a blank line; explain arguments, results, and side effects when needed. These are content guidelines, not syntax requirements.

In the example, square.__doc__ exposes the description, while square(5) computes its result independently of the documentation. The -OO option removes docstrings, so application code should not depend on their presence.

Examples

def square(number):
    """Return the square of number."""
    return number * number

print(square.__doc__)
print(square(5))

Result

Return the square of number.
25

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources