You are currently viewing docstring python example code step by step
Docstring Python Example Code

docstring python example code step by step

Spread the love

What are docstrings in Python with example?

Python documentation string is commonly known as docstring operator. It is a string literal and used in the module definition, class, function, method or description. Docstrings are declared using ”’ triple single quotes ”’ or ” ” ” triple double quotes ” ” ” and are accessed through the doc attribute (__doc__) and built-in help() function for any of the Python objects. An object’s docstring is defined by including a string constant as the first statement in the object’s definition.

Docstring Python Example Code
Docstring Python Example Code

Example:-

Code:-



def demo():

 '''Demonstrates triple double quotes docstrings using doc attribute

    	(__doc__) and built-in help() function '''

    	return(0)

  

print("Using __doc__:")

print(demo.__doc__)

  

print("Using help:")

help(demo)



Output:-



Using __doc__:
Demonstrates triple double quotes docstrings using doc attribute

    (__doc__) and built-in help() function 

Using help:

Help on function demo in module __main__:



demo()

    Demonstrates triple double quotes docstrings using doc attribute

    (__doc__) and built-in help() function

Difference between docstrings and comments

Docstrings are similar to commenting, but they are a more logical, enhanced, and useful version of commenting while on the other hand, comments are mainly used to explain portions of the code which can not be understood easily and comments can also be useful for comments on Fixing bugs and tasks that need to be done.

Docstrings are represented with opening and closing quotes while remember that comments start with a # at the beginning.

Example:-

For comment:-



Code:-

# Python program to demonstrate comments

print("Comment")



Output:-

Comment



For docstring:-



Code:-

'''Python program to demonstrate comments'''

print("Docstring")



Output:-

Docstring



There are a couple of ways of writing or using a docstring, i.e., one-line or single-line docstring and multi-line docstring. 



Single-line docstrings in Python

Single-line docstrings are the string literal that fits in one line.

Example:-



Code:-

def demo():

'''Demonstrates Single-line docstrings.'''

    	return(0)

  

print("Using __doc__:")

print(demo.__doc__)



Output:-

Using __doc__:

Demonstrates Single-line docstrings.

Standard conventions to write single-line docstrings:

● They are single-lined docstrings, but we use the triple quotes around these docstrings as they can be expanded easily later.

● The opening quotes and closing quotes are on the same line.

● There’s no blank line either before or after the docstring in python.

● They should not be descriptive on nature, but it must follow “Do this, return that” structure ending with a period.

Multi-line docstrings in Python

Multi-line docstrings consist of a brief summary of a line just like a one-line docstring, followed by a blank lines, followed by a more elaborate description in python.

Example:-

Code:-

def demo():

    '''Summary line.

  

    	Description of function.



Demonstrates Multi-lined docstrings.'''

    	return(0)

  

print("Using __doc__:")

print(demo.__doc__)



Output:-

Using __doc__:

Summary line.

  

    	Description of function.

    

    	Demonstrates Multi-lined docstrings.

Summary : In this article we saw What Are Docstrings In Python With Example? if about this article any query then free to ask me

Intern name who share the Task: Shyamal Krishna Agrawal

Best of luck !!!!

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

sachin Pagar

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply