A comment is a piece of text within a program that is not executed. It can be used to provide additional information to aid in understanding the code.
In Python, the
# character is used to start a comment. The comment continues after the
# until the end of the line.
# Comment on a single linename = "Pied Piper" # Comment after code
Python does not have a specific syntax for multi-line comments, unlike some other languages.
# characters can be used:
# This is a comment written over# more than one lineprint("Hello, World!")
Another, less official way of writing comments in Python is to use a multi-line string. Python will ignore string literals that are not assigned to a variable, so multi-line strings (created by surrounding text with triple quotes
""") can be used as de facto comments:
"""This is a string written overmore than one line"""print("Hello, World!")
Here are some examples of comments in a Python program:
- Anonymous contributors
Learn Python on Codecademy
- Anonymous contributorsAnonymous contributors