In this tutorial, learn how to add Python single line comment. You can also add a multiline comment on your Python file or code. The short answer is to use hash(#) before any text or code to comment out.
Commenting out a line or add a comment to your Python file is a good practice for developers. By adding a line of comment for each working of code. Makes it easy for programmers to analyze any code in just one view.
Python Single Line Comment
You can add a single line of comment to your python file or Python code. To add any comment to your Python file, you have to use symbol hash(#). A hash(#) symbol comment out only a single line of text on Python.
Add single line description text to your Python file using hash(#) symbol. Describe useful codes or any working of codes with a single line of comment.
See the added comment in the below code.
1 2 |
#Print method to print text print("Hello World!"); |
Output
The commented code will not display in the output section of Python. The above example contains the comment in the code section of Python. However, the output contains no commented codes.
How to Comment Python Codes
If you have to comment out a line of code, you have to use the same hash(#) symbol. Place the hash(#) symbol before the start of code which you want to make comment.
1 2 |
#print("Hello World!"); print("This is simple text. The above text is commented."); |
Output
The above code contains the commented code. The commented code will not get execute also will not display in the output.
Write Multiline Comment in Python
If you want to comment out multiple lines of code in Python. You have to put symbol triple quotes(“””) to the start and end of the line. Make text commented or comment out the multiple lines of codes using this symbol
1 2 3 4 5 |
print("All other text are commented here."); """print("This is text one."); print("This is text two."); print("This is text three."); print("This is text fore.");""" |
Output
The above example contains the multiple commented print statement. Only the first print statement will be displayed in the output section.
If you have not installed Python yet, you have read a post on how to download Python on Windows.
You may also like to read.
Hope you like this tutorial of how to add Python single line comment and multiline comment. If you have any query regarding the tutorial, please comment below.
Also tell me, which method you are using to comment out the Python codes.