Python Comments Multiple Lines


There are two ways in which you can comment out multiple lines in Python Programming,

  1. Multi-Line Comment: using triple single quotes: '''
  2. Docstring: using triple double quotes: """

Examples:

  • Using triple single quotes at the start and end of the command line.
    '''
    This is a multi-line comment
    using triple single quotes
    that can span multiple lines.
    '''
    
    # This is a single-line comment
    
    def my_function():
     ...
     ...
     ...
    
  • Using triple double quotes at the start and end of the command line.
    def my_func():
        """
        This is a multi-line comment
        using triple single quotes
        that can span multiple lines.
    
        This is called the function-level docstring.
        It is used to document the purpose and behavior of a function.
        """
        ...
        ...
        ...
    

Real World Example:

'''
Developer: Code2care
Date: 25 April 2023
Version: v1.0
'''

def calculate_simple_interest(principal, rate, time):
    """
    This function calculates the simple interest 
    for a given principal amount, rate of interest, and time.

    :param principal: The principal amount.
    :param rate: The rate of interest per year.
    :param time: The time duration in years.
    :return: The simple interest calculated for the given parameters.
    """
    interest = (principal * rate * time) / 100
    return interest

#function call
print("The Simple Interest is: ",calculate_simple_interest(1000,4,12))
Muli-Line Comments in Python

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap