Skip to content

Beginner · First steps in Python · topic 2 of 50 · 15 min

print() and comments

Explanation

print() shows values on the screen. You can print text, numbers, or several things at once separated by commas. A comment starts with # and is a note for humans; Python ignores it.

Printing text and numbers

print is a function. A function is a named action you can use. You use (or "call") a function by writing its name followed by round brackets. Whatever you put inside the brackets is what print shows.

python
print("Good morning")
print(42)
print(10 + 5)
Text needs quotes. Numbers do not. Python works out 10 + 5 before printing.

Printing several things at once

Separate items with commas. print puts one space between them automatically.

python
print("Total:", 450, "rupees")
Shows: Total: 450 rupees

Changing the separator and the ending

print has two optional settings. sep is what goes between items (a space by default). end is what goes after the last item (a new line by default).

python
print("2024", "06", "15", sep="-")
print("Loading", end="...")
print("done")
Shows 2024-06-15, then Loading...done on one line.

Comments: notes for humans

A comment is text that Python skips. It starts with the # sign and runs to the end of the line. Use comments to explain why your code does something.

python
# Price of one samosa in rupees
print(15)  # this comment is at the end of a line
Python only runs print(15). Both comments are ignored.
Think of it like this: Comments are like the small notes a teacher writes in the margin of your notebook. They help the reader understand, but they are not part of the answer itself.
Careful: Text without quotes is not a string. print(Hello) gives a NameError, because Python thinks Hello is the name of a variable that does not exist. Write print("Hello").
Tip: You can use single quotes or double quotes: print('Hi') and print("Hi") are the same. Pick one style and stay with it.

Printing versus returning

In the practice tasks on this site you will often write a function that returns a value instead of printing it. Returning hands the value back so the checker can test it. You will learn functions fully later; for now, read return as "give back this answer".

Watch the lesson

print() and comments · 1/6

print() and # comments

In this lesson you will learn to show things on the screen with print, and to leave notes in your code with comments.

Prefer a video by a teacher? Find free videos on “python print function and comments tutorial” on YouTube ↗

In real life: Printing a restaurant bill

Four friends eat at a dhaba. The owner wants a neat bill on screen with the item names and prices. print with commas and sep makes a clean, readable bill in a few lines.

python
print("Paneer tikka", 220, sep=" .... Rs ")
print("Butter naan x4", 160, sep=" .... Rs ")
print("Lassi x4", 200, sep=" .... Rs ")
print("Total", 580, sep=" .... Rs ")
sep lets you control what goes between the item and the price.

Practice (0/2 done)

Task 1 · Code · Python

Write a function bill_line() that returns the string "Samosa: 15 rupees". (Return the text; do not print it.)

Task 2 · Code · Python

Someone turned an important line into a comment by mistake, so answer() returns 0. Remove the # from the right line so that answer() returns 42. Keep the first comment, it is a useful note.

Quick quiz

Question 1

1.What does print("A", "B", "C") show?

Question 2

2.What does print("10", "20", sep="/") show?

Question 3

3.Which symbol starts a comment in Python?

Question 4

4.What happens when you run print(Hello) with no quotes?

Question 5

5.By default, what does print add after the last item?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: What programming and Python are