How do we create multiline comments in Python?

Python | Multiline Comments: Here, we are going to learn how do we create multiline comments to comment a section in Python?
Submitted by IncludeHelp, on June 12, 2020

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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.