Docstring

String literal that provides documentation per computer programming language syntax


title: "Docstring" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["programming-constructs", "lisp-(programming-language)", "python-(programming-language)", "source-code-documentation-formats", "string-(computer-science)"] description: "String literal that provides documentation per computer programming language syntax" topic_path: "technology/computing" source: "https://en.wikipedia.org/wiki/Docstring" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary String literal that provides documentation per computer programming language syntax ::

A docstring is a string literal that annotates an associated section of source code. It provides for the same utility as a comment, but unlike a comment is a string literal and is retained as part of the running program. Some development tools display docstring information as part of an interactive help system.

Programming languages that support docstring include Python, Lisp, Elixir, Clojure, Gherkin, Julia and Haskell. Tools that leverage docstring text include cobra-doc (Cobra), doctest (Python), Pydoc (Python), Sphinx (Python).

Examples

Elixir

Documentation is supported at language level, in the form of docstrings. Markdown is Elixir's de facto markup language of choice for use in docstrings: ::code[lang=elixir] def module MyModule do @moduledoc """ Documentation for my module. With formatting. """

@doc "Hello" def world do "World" end end ::

Lisp

In Lisp, a docstring is known as a documentation string. The Common Lisp standard states that a particular implementation may choose to discard docstrings. When they are kept, docstrings may be viewed and changed using the DOCUMENTATION function. For instance: ::code[lang=lisp] (defun foo () "hi there" nil) (documentation #'foo 'function) => "hi there" ::

Python

In Python, a docstring is a string literal that follows a module, class or function definition. It must be nothing but a string literal, not any other kind of expression. The docstring is accessible via the associated code element's __doc__ attribute and the help function.

The following Python code declares docstrings for each program element:

::code[lang=python] """The module's docstring"""

class MyClass: """The class's docstring"""

def my_method(self):
    """The method's docstring"""

::

If saved as , the following is an interactive session showing how the docstrings may be accessed:

::code[lang=pycon]

import mymodule help(mymodule) The module's docstring help(mymodule.MyClass) The class's docstring help(mymodule.MyClass.my_method) The method's docstring ::

References

References

  1. "Function definition with docstring in Clojure".
  2. "Step Arguments - Doc Strings".
  3. "Documentation — Julia Language 0.4.1 documentation".
  4. "Docstrings".
  5. [http://www.lispworks.com/documentation/HyperSpec/Body/f_docume.htm CLHS: Standard Generic Function DOCUMENTATION...]

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

programming-constructslisp-(programming-language)python-(programming-language)source-code-documentation-formatsstring-(computer-science)