Doc strings is the documentation string which is string literal, and it occurs in the class, module, function or method definition, and it is written as a first statement.
There are two types of a Doc strings:
These are mainly used in DataScience /Machine Learning Programming.
This type of Doc strings fit in one line. You can write them either using single quotes( ' ... ') or triple quotes ('''....''') .
in the following program we are declaring a doc string with in a function, and printing its contents.
def square(x): '''Returns argument x is squared.''' return x**x print (square.__doc__) help(square)
Returns argument x is squared. Help on function square in module __main__: square(x) Returns argument x is squared.
The Multi-line Doc Strings is similar to One-line Doc Strings the only difference is, after the first line of the multi-line doc strings we leave a single blank line, followed by descriptive text before closing it.
def function(arg1): """Explanation of the function Parameters: arg1 (int): Description of arg1 Returns: int:Returning value """ return arg1 print(function.__doc__)
As you can observe that the sentence "Explanation of the function" is separated from other contents of the comment by a single blank line.
Explanation of the function Parameters: arg1 (int): Description of arg1 Returns: int:Returning value