Skip to main content

Comments in Python

Submitted by admin on Wed, 12/08/2021 - 13:02
Comments in Python

What are comments

Like other programming languages, Python also support commenting codes. Comments are not read or executed as part of the program, rather they are used to add notes / explanation in code.

Single-line comment in Python

Just add # at the beginning of the line.

Example


# This is a comment line.
print("Ravi Shekhar") # we can also add comment after code.

Multiple-lines comments in Python

There are two ways you can write multiple lines codes.

  1. Put # at the start of every line.
  2. Using triple quotes.

How to use triple quotes for multiple lines comments

Just wrap your comment inside a set of triple quotes.


"""
This is first line of multiple lines comment.
This is another comment line.
This is third comment line.
"""


'''
This is first line of multiple lines comment.
This is another comment line.
This is third comment line.
'''

How to add # at the start of multiple lines by one click

Select the lines that you want to comment (add # at the start of the line) and press:

  • On Windows: Ctrl + /
  • On Mac: cmd + /

I hope this python tutorial was helpful to you.

Category

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.