×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

How do we create multiline comments in Python?

By IncludeHelp Last updated : December 07, 2024

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

Comments and Discussions!

Load comments ↻





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