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 MULTILINE COMMENT INPYTHON*/
But there are still a couple of workarounds to comment multiple lines in Python. Let’s have a look at them!
#
line commentsYou 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.
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 COMMENTUSING STRING LITERALS!'''print("Educative")for i in range(10):to_print = str(i) + " Educative"'''DO NOT FORGET TO PROPERLYINDENT THE STARTING OF STRINGLITERALS WITHIN YOUR CODE! '''print(to_print)
Free Resources