In this tutorial, we will learn about python docstring, where and why python docstrings are used.
What is Python Docstring?
Python docstring or Documentation strings is a string literally used in the class, module, function, or method definition.
As like multiline comment, docstring is also declared using three (‘’’) or four (“””). For example
‘’’ triple single quotes ‘’’ or “”” triple double quotes ”””
Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and built-in functions. Docstrings are great for understanding the functionality of the more extensive code of the project.
Example of Code:
To run this docstring code, we have to follow this step.
Here the output of string literal.
Here, we have documented our addition function, and then we are accessing it with __doc__ attribute.
Python Docstring in built-in functions
Now let’s use docstring for the built-in python function and let it have a print function, for example.
As we can see, we got the documentation output of the print() function defined by python.
Docstring in Python Module