Google News
logo
Python Program to Docstrings
Here is an example Python program that demonstrates how to use docstrings in your code:
Program :
"""This is a
multiline docstring."""
print("Hello, World!")
Output :
Hello, World!


Another Example of Docstrings :

Program :
def calculate_area(radius):
    """
    This function calculates the area of a circle.

    Parameters:
        radius (float): The radius of the circle.

    Returns:
        float: The area of the circle.
    """
    import math

    # Calculate the area using the formula: A = πr^2
    area = math.pi * radius ** 2

    return area

# Call the function and print the result
print(calculate_area(5))
Output :
78.53981633974483
In this program, we use docstrings to explain what the calculate_area function does and how it works. Docstrings in Python are enclosed in triple quotes ('"""') and are typically placed at the beginning of a function, class, or module. They are used to provide documentation for your code and help other developers understand how to use it.

The docstring for the calculate_area function explains that it calculates the area of a circle and takes a single parameter, radius, which should be a float. It also specifies that the function returns a float, which is the area of the circle.

By using docstrings in your code, you can create more robust and maintainable programs that are easier to understand and use by other developers.