Comments

habkhyar's avatar
Published May 3, 2021Updated Oct 7, 2024
Contribute to Docs

A comment is a piece of text within a program that is not executed. It is used to provide additional information or context to help understand the code. Python uses the # character to start a comment, which extends to the end of the line.

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 line
name = "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 line
print("Hello, World!")

Alternatively, multi-line strings (with triple quotes """) can also be used as de facto comments. These are ignored by Python if not assigned to a variable:

"""
This is a string written over
more than one line
"""
print("Hello, World!")

Codebyte Example

Run the following codebyte to understand how comments work in Python:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy