How to add single-line comment in Python

What is comment in Python?

In Python, a comment is a piece of text in code that is not executed when the program is executed.

Comments are used to provide explanations, and keep documentation, or notes within the code for future reference and code understanding.

Python supports two types of comments: single-line comments and multi-line comments (docstrings).

Creating single-line comments in Python

Just add a hash (#) before a line to comment out the line.

In the following example, a hash has been added before the line 'This is an example of a single-line comment'. Python interpreter will ignore the line as comment and it will not affect the execution of the program.

Please take a closer look at the following example:

Example of Python Single-line Comment


Output of the above example

Learning Python is a fun!

The output does not contain the commented line.

More Single-line Comment

In Python, single-line comment can be placed at the end of a line of code. Anything after hash (#) is treated as comment.

In the following example, a comment (# This is another example of a comment) has been added at the end of Python code print("Learning Python is a fun!") which is in the same line. Python interpreter will ignore the line as comment and it will not affect the execution of the program.

Let's take a closer look at the following example:

Example of Comment at the End of a Python Code Line


Output of the above example

Learning Python is a fun!

The output does not contain the commented lines.

Live Code Playground

In the following Python code editor, you can practice the above code by updating or adding more comment with hash(#). Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output