In Python, you can simulate a multiline comment using triple quotes (either single or double) at the beginning and end of the text. Technically, these are string literals, not true comments. If left unassigned, Python ignores them at runtime. Here's an example:
Simulate multiline comments in Python using three double-quotations
python
"""This is amultiline commentprint("this will not be printed")"""print("but this will be")
You can place these string literals before or after a statement or group of statements, or on their own line. While often used to document code or add notes for other developers, remember they are technically string literals. For formal documentation of functions or classes, Python uses docstrings (triple-quoted strings placed immediately after a definition).
For actual comments, use single line comments with #, which will comment out the rest of the line.
Create single line comments in Python using the hash sign
python
# This is a single line comment# print("Not to be printed")print("Hello World") # This is also a single line comment
How do I create multiline comments in Python?
In Python, you can simulate a multiline comment using triple quotes (either single or double) at the beginning and end of the text. Technically, these are string literals, not true comments. If left unassigned, Python ignores them at runtime. Here's an example:
Simulate multiline comments in Python using three double-quotations
You can also use triple single quotes (
''') to simulate a multiline comment, like this:Simulate multiline comments in Python using three single-quotations
You can place these string literals before or after a statement or group of statements, or on their own line. While often used to document code or add notes for other developers, remember they are technically string literals. For formal documentation of functions or classes, Python uses docstrings (triple-quoted strings placed immediately after a definition).
For actual comments, use single line comments with
#, which will comment out the rest of the line.Create single line comments in Python using the hash sign