Comments
Anonymous contributor
Anonymous contributor23 total contributions
Anonymous contributor
Published May 3, 2021Updated Sep 9, 2021
Contribute to Docs
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.
Single-line Comments
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
Multi-line Comments
Python does not have a specific syntax for multi-line comments, unlike some other languages.
Instead, multiple #
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!")
Codebyte Example
Here are some examples of comments in a Python program:
All contributors
- Anonymous contributorAnonymous contributor23 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributor
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.