Skip to main content

Creating a Comment

A comment begins with #, and Python skips over it. You can put one at the end of a line of code, and you can also use one to stop code from running.

Comment before code

Prompt
Write a comment on its own line, then print "Hello, World!".
Output: complete Python program

End of line comment

At the end of a line, a comment makes Python skip everything after the #:

Prompt
Print "Hello, World!" with a comment at the end of the same line.
Output: complete Python program

Prevent execution

Placing # in front of a line of code stops Python from running it:

Prompt
Comment out a print statement for "Hello, World!" and print "Cheers, Mate!" instead.
Output: complete Python program

Multiline Comments

Python has no dedicated syntax for comments that span several lines. One option is to start each line with its own #. Another is a multiline string in triple quotes: a string literal that isn't assigned to a variable gets ignored, so your comment can safely sit inside one.

Hash on each line

Prompt
Write a multiline comment using # on each line, then print "Hello, World!".
Output: complete Python program

Triple quotes

A multiline string in triple quotes can act as a comment. Python reads it and then ignores it:

Prompt
Use a triple-quoted multiline string as a comment, then print "Hello, World!".
Output: complete Python program