Docstrings in Python
In this lesson, you will learn about python docstrings in depth and how to use it in your python program with examples.
Docstrings in Python
Docstrings or Python documentation strings are used after the definition of a function, method, class, or module. It provides an efficient method of associating documentation with Python modules, functions, classes, and methods.
Docstrings in python, use triple quotation marks for documentation. It is similar to commenting in python, docstrings are used to document a specific segment of code.
Conditions for using docstrings:
- The first line should be a short description and it should begin with a capital letter and end with a period.
- When the documentation has multiple lines, the second line should be an empty line.
Declaring Docstrings
Docstrings in python use triple quotation marks for the documentation of all modules, functions, classes, and methods.
Single-line docstrings in Python
Single line docstrings are the documents that fit in one line.
Multi-line Docstrings in Python
Multi-line docstrings consist of a full detailed summary like document, where the first line is a one-line docstring, followed by a blank line, and by a more detailed description.
How to create multiline Docstrings in python?
Look at the below example.
[gamipress_button label=”Try It” onclick=”window.open(‘https://coderseditor.com/?id=1383′,’_blank’)”]
def docstring_function(): print('Developer Publish Academy') ("""About Developer publish academy""") ('''Developer publish academy teaches python programming. Python programming course in Developer publish academy is the best in the market. All the lessons are neatly drafted and are very much understandable. You can learn programming easily by self-learning the course''') docstring_function()
Here, the docstrings have started just after the function definition and it gives a detailed note. We have given a random description in this example, but you can provide an exact detailed note for the function. It helps the other users to identify the use of the function or the logic of the program.