Getting started with Python
Output and comments
Use print() to display text and comments to explain code.
Lesson 4 of 63
Reading: about 2 min
Lesson + practice: about 8 min
Difficulty: Beginner
On this page4 sections
The print() function displays a value in the terminal. Put text inside matching quotation marks. Each print() call below displays a new line.
print("I am learning Python.")
print("I can run a script.")I am learning Python. I can run a script.
Add a comment
A comment begins with #. Python ignores the comment when it runs the script. Comments can explain why a piece of code exists.
# My first Python program
print("Comments do not appear in the output.")Comments do not appear in the output.
Why this works
print() displays the value of an expression. A quoted string is text; Python shows its contents without the surrounding quote marks. A comment is for a reader of the source file and is ignored by the interpreter, so it cannot replace a print() call when you need visible output.
Practice
Add two print() lines and one comment to hello.py. Save and run the file. Check that the comment does not appear in the output.
Will a comment starting with # be printed?
Reveal answer
No. Python ignores comments; only the print() calls display output.
Try another case
What does print("# note") display, and why?
Check your answer
Reveal answer
It displays # note because the # is inside the quoted string.
Your progress
Reveal the check answer or reach the end to complete this lesson.