What Is A Docstring In Python?
Published on October 07 2023
In this tutorial, we will learn what docstring in Python is and how to use it. This tutorial is part of Python's top 25 most commonly asked interview questions.
Definition
- Documentation string or docstring is a multiline string used to document a specific code segment.
- Python lets users include a description for their methods using docstrings that occur as the first statement in a module, function, class, or method definition.
- These docstrings are within triple quotes.
- The docstring should describe what the function or method does.
- __doc__ or help() can be used to access document strings.
- They are not assigned to any variable and therefore, at times, serve the purpose of comments as well (Docstrings are not actually comments).
Example
- One line docstrings
def hello():
""" A function to print message. """
return "hello"
print(hello.__doc__)
Output
A function to print message.
- Multi-line docstrings
"""
Using docstring as a comment.
"""
To know about it, please refer to the official documentation website - official website.
Next Tutorial
- What Is Python, Its Benefits And Features?
- What Is Pickling And Unpickling In Python?
- Memory Management In Python
- What Is The __Init__ In Python?
- Difference Between List And Tuple
- What Is The Self Keyword In Python?
- What Is The Lambda Function In Python?
- What Is A Break, Continue, And Pass Statement In Python?
- What Are The Common Built-In Data Types In Python?
- What Is The Difference Between .Py And .Pyc Files In Python?
- What Are Decorators In Python?
- What Is Slicing In Python?
- What Are Negative Indexes In Python?
- What Is List And Dictionary Comprehension In Python?
- What Is A Docstring In Python?
- What Are Python Iterators?
- What Are *Args And **Kwargs In Python And How To Use Them?
- What Is The Pythonpath Variable In Python?
- What Are Namespaces In Python?
- What Is Pep8 (Python Enhancement Proposals) In Python?
- What Is The Split(), Join(), Sub(), And Subn() Method In Python?
- What Are Modules And Packages In Python?
- What Is Scope Resolution In Python?
- Is Python Compiled Or Interpreted Language?
- What Are Generators In Python?