How to Comment in Python?

In Python, comments are used to make single or multiple lines of code more readable understandable. Comments are used to describe code functionality and are also used along with the part of code that needs to be ignored while executing. The code block that comes within the comments section will be ignored by the interpreter while execution.

This write-up will give you a detailed understanding of comments and how to create comments in Python with numerous examples. The following aspects are demonstrated in this Python blog post: 

Let’s start!

What is a Comment in Python?

Comments describe the functionality or working of code in a short way along with code. Comments make the program more understandable and readable. Comments help the viewers/readers to grasp the concept more clearly.

Comments are initialized in Python using the hash “#” symbol. To avail the functionality of the comments, specify the “#” symbol at the start of each line of code. Comments are initialized as a single line or multiline in Python script.

Let’s discuss the detail of comments using examples:

Example 1: How to Create Single-Line Comments?

In the example code shown below, the single-line comment is initialized inside the Python script using the hash symbol “#”.

Code:

# printing name of the student
print('Alex')
print('Hello') #printing a string

In the above code:

  • The single line comment is initialized at the start using the hash symbol “#”.
  • The print() function displays a string value “Alex” on the screen.
  • Python interpreter ignores everything that comes after symbol hash “#”.

Output:

From the above output, it is clear that anything written in the comment is not executed.

Example 2: How to Create Multi-Line Comments?

In the example code shown below, the multi-line comment is initialized inside the Python script using the hash symbol “#”.

Code:

# its Linux foss Python Guide
# Printing Name of the Student
# Printing a String
print('Alex')
print('Hello')

In the above code:

  • The multi-line comments are initialized using the “#” hash symbol.
  • Each line with a hash symbol at the start is considered a comment, and all these lines will be ignored while executing the code.

Output:

From the above output, it is verified that all the comments are ignored by the Python interpreter.

Example 3: Using String Literals to Create Single and Multi-Line Comments

In the example given below, the string literals are used for single and multiple-line comments. This method is not technically commented, but if the string is not assigned to any variable or function in Python, they are ignored by the Python interpreter. That’s the reason many programmers use this method for comments while coding. Let’s look at the following example:

Code:

'its Linux foss Python Guide'

'''
Printing Name of the Student
Printing a String

'''
print('Python')

In the above code:

  • The string literals are used for single lines and more than one line comments.
  • For single-line comments, single quotations are used, as Python ignores the string if they are not assigned to any variable.
  • Similarly, for multiple lines, triple quotation marks are used in Python.

Output:

From the above output, it is verified that all the comments are ignored by the Python interpreter.

All right, that’s all for now!

Conclusion

To comment in Python, a hash symbol “#” is used at the start of any code line. “String literals” are also utilized to make comments in Python script. Using these two methods, single-line and multiple-line comments can be created in Python. Python ignores string literals if not initialized to a variable or function so that the string literals can be used as comments in Python. This post briefly discussed comments, single-line comments, and multi-line comments in Python with their examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×