Python Keywords, Identifiers and Statements 6

Docstring in Python

* Docstring is short for documentation string.

* Triple quotes are used while writing docstrings.

* It is a string that occurs as the first statement in a module, function, class, or method definition.

* We must write what a function/class does in the docstring.

For example:

#function definition

def sum():
    """
    calculate and print sum of two numbers
    """
    a=int(input("Entr 1st no "))
    b=int(input("Enter 2nd no "))
    c=a+b
    print("Sum = ",c)

#function calling
sum()

To print doc string

Docstring is available to us as the attribute __doc__ of the function. Inorder to print doct string we have issue the command as

syntax:
print(functionname.__doc__)

Note:two underscores before and after doc

>>> print(sum.__doc__)

calculate and print sum of two numbers

>>>

>>> print(sum.__doc__)

    calculate and print sum of two numbers
    
>>> 

Visitor Counter

0056736