Home » Tutorials » Python » Comments

Comments

Commenting in Python is also quite different than other languages, but it is pretty easy to get used to. In Python there are basically two ways to comment: single line and multiple line. Single line commenting is good for a short, quick comment (or for debugging), while the block comment is often used to describe something much more in detail or to block out an entire chunk of code.

One Line Comments

Typically, you just use the # (pound) sign to comment out everything that follows it on that line.

Example
print("Not a comment")
#print("Am a comment")
Result

Not a comment

Multiple Line Comments

Multiple line comments are slightly different. Simply use 3 single quotes before and after the part you want commented.

Example
'''
print("We are in a comment")
print ("We are still in a comment")
'''
print("We are out of the comment")
Result

We are out of the comment

Alright, we are done with comments, but don’t forget them. They are your best friend in debugging complex Python code. Now onto the actual programming stuff.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Comments". After Hours Programming. Accessed on April 23, 2024. https://www.afterhoursprogramming.com/tutorial/python/comments-py/.

  • Stewart, Suzy. "Comments". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/comments-py/. Accessed 23 April, 2024.

  • Stewart, Suzy. Comments. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/comments-py/.



52 thoughts on “Comments”

  1. print(“not a comment”)
    #print(“Am aa comment”)

    “‘
    print(“We’re in the comment”)
    print(“we’re still in thew comment”)
    “‘
    print(“out a comment”)

  2. print(“not a comment”)
    #print(“Am aa comment”)

    “‘
    print(“We’re in the comment”)
    print(“we’re still in thew comment”)
    “‘
    print(“out a comment”)

  3. So comments for python is like a hidden define to tell the user after over 1k lines what this part of the code is responsible for? Random question how did you get the 3 quote signs?

  4. Kudos, extreme kudos sir, for adding a code simulator that is actually accessible to screenreaders. This fact alone has blown away big contenders like CodeAcademy for me, a blind screenreader user. Keep rocking 🙂

  5. In Python 3.4.3 shell has the syntax for multiple line comments changed? This is what I got:
    >>> ”’
    print(“We are in a comment”)
    print (“We are still in a comment”)
    ”’
    print(“We are out of the comment”)
    SyntaxError: multiple statements found while compiling a single statement
    >>>

    Also when trying only multiple comments without including the print command, I get this:
    >>> ”’
    print(“We are in a comment”)
    print (“We are still in a comment”)
    ”’

    print(“We are in a comment”)
    print (“We are still in a comment”)

    >>>

  6. ”’
    print(“We are in a comment”)
    print(“We are still in a comment”)
    ”’
    displayed this

    print (“We are in a comment”)
    print (“We are still in a comment”)
    ‘ while print(“We are out of the comment”) print the comment separately. why not together?

  7. The code simulator on this page doesn’t recognize blank lines,
    or tab or print(‘ ‘).

    ”’
    Show 2 blank lines before printing to the console “Two Blank Lines”
    ”’

    print(”

    Two Blank Lines”)

  8. Ok I tried using 3 pairs of quotes (which works as well) and I get this now:

    “””

    print(“We are in a comment”)
    print (“We are still in a comment”)
    “””

    print(“We are out of the comment”)

    print(“We are in a comment”)
    print (“We are still in a comment”)

  9. So I typed this into IDLE

    ”’
    print(“We are in a comment”)
    print (“We are still in a comment”)
    ”’ print(“We are out of the comment”)
    SyntaxError: invalid syntax

    and I get a syntex error, anybody have any tips?

  10. print("hi")
    print("how are you")
    print("i am happy that i learned how to use some code of python language")
    print("very nice tutorial")
    print("^_^")

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.