Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

comment
python
string
triple-quoted

Multiline comment in Python

Educative Answers Team

Unlike many other programming languages, Python does not have an out of the box multiline commenting syntax. So the following won’t work in Python:

/*
THIS IS A MULTI 
LINE COMMENT IN 
PYTHON
*/

But there are still a couple of workarounds to comment multiple lines in Python. Let’s have a look at them!


1. Using multiple single # line comments

You can use # in Python to comment a single line:

# THIS IS A SINGLE LINE COMMENT

Using # consecutively, you can also comment multiple lines, like this:

# THIS IS A MULTILINE
# COMMENT IN PYTHON
# USING THE SINGLE LINE
# COMMENT CONSECUTIVELY

But having to comment out each line of a multiline comment individually can be a very tedious process, especially if you are not using a modern editor.


2. Using triple-quoted string literals

Another way to add multiline comments is to use triple-quoted, multi-line strings. These strings are to be used carefully and should not be confused with Docstrings (triple-quoted string literals appearing right after a function/class/module to generate documentation).

String literals can be used within the code to insert multiline comments and do not generate any code.

Note: Even though triple quoted string literals do not generate any code, they are treated as code and should be properly indented within blocks to work correctly!

'''
THIS IS A MULTILINE COMMENT
USING STRING LITERALS!
'''
print("Educative") 

for i in range(10):
  to_print = str(i) + " Educative"
  
  '''
  DO NOT FORGET TO PROPERLY
  INDENT THE  STARTING OF STRING 
  LITERALS WITHIN YOUR CODE! '''

  print(to_print)

RELATED TAGS

comment
python
string
triple-quoted
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring