How do we create multiline comments in Python?

Learn, how do we create multiline comments to comment a section in Python?
Submitted by IncludeHelp, on June 12, 2020 [Last updated : March 14, 2023]

Introduction

When we need to comment on multiple lines/statements, there are two ways to do this, either comment each line or create multiline comments (or block comment).

In C / C++, we use /*...*/ for the multiline comment, but in Python programming language, we have two ways to comment a section.

Python - Multiline Comments

The first way is to comment on each line,

This way can be considered as a single line comment in Python – we use the hash character (#) at the starting of each line to be commented.

# This is line 1
# This is line 2
# This is line 3

And, the second way is to comment the section,

This way can be considered as a multiline comment in Python – we use triple single quotes (''') at the starting of the section to be commented and again triple single quotes (''') at the end of the second.

'''
This is line 1
This is line 2
This is line 3
'''

A Python example for creating multiline comment

'''
Function name: print_text
Parameters: None
Return type: None
Description: This function will print 
some text on the screen
'''

def print_text():
  print("Hello, world! How are you?")

if __name__ == "__main__":
  # Here, we will call the print_text function
  # that will print some text on the screen
  print_text()

Output

Hello, world! How are you?

In the above program, there are two multiline sections which we is commented,

Section 1:
'''
Function name: print_text
Parameters: None
Return type: None
Description: This function will print 
some text on the screen
'''

Section 2:
# Here, we will call the print_text function
# that will print some text on the screen

Python Tutorial

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.