print() is often the first function you learn and one of the most useful debugging tools. It writes values to standard output and adds a newline by default.
Printing values#
Pass one or more values to print():
print("Python")
print("Hello", "World")
print(42)Python Hello World 42
When you pass several arguments, Python inserts a single space between them. That is controlled by the sep keyword argument.
print("2026", "11", "19", sep="-")
print("a", "b", "c", sep="")2026-11-19 abc
Controlling the line ending#
print() normally ends with a newline. Set end to change that:
print("Loading", end="...")
print("done")Loading...done
This is useful for building one line from a loop:
for number in range(5):
print(number, end=" ")
print()Comments#
A comment starts with # and continues to the end of the line. Python ignores it.
# Calculate the total price
price = 19.99
quantity = 3
total = price * quantity # tax is not included
print(total)Use comments to explain why code does something, not to restate what the code already says. A comment such as # add one to count above count += 1 adds noise.
Multi-line notes#
Python does not have a dedicated block-comment syntax. By convention, triple-quoted strings are used for documentation strings (docstrings) inside functions, classes, and modules:
def area(width, height):
"""Return the area of a rectangle."""
return width * heightA docstring documents a public object and can be read with help(area). A bare triple-quoted string in the middle of unrelated code is usually not the best choice; several # lines are clearer.
Practice#
Predict the output before running this program:
print("One", end=" ")
print("Two")
print("Three", "Four", sep=" | ")Then run it in the compiler and compare. Change sep and end to see how the output changes.
