Python Basics — print() Function
print() Function
The print() function displays output to the console. It’s the most basic and most frequently used function in Python.
Basic Printing
print("Hello, World!") # Hello, World!
print(42) # 42
print(3.14) # 3.14
print(True) # True
print([1, 2, 3]) # [1, 2, 3]
Printing Multiple Values
Pass multiple arguments separated by commas:
name = "Alice"
age = 25
print("Name:", name, "Age:", age)
# Name: Alice Age: 25
By default, print() separates values with a space.
The sep Parameter
Change the separator between values:
print("apple", "banana", "cherry")
# apple banana cherry
print("apple", "banana", "cherry", sep=", ")
# apple, banana, cherry
print("2025", "03", "15", sep="-")
# 2025-03-15
print("Hello", "World", sep="---")
# Hello---World
print(1, 2, 3, 4, 5, sep=" | ")
# 1 | 2 | 3 | 4 | 5
The end Parameter
By default, print() adds a newline (\n) at the end. Change it with end:
# Default behavior — each print on a new line
print("Hello")
print("World")
# Hello
# World
# Custom end — print on the same line
print("Hello", end=" ")
print("World")
# Hello World
# No newline
print("Loading", end="")
print("...")
# Loading...
# Custom end character
print("Step 1", end=" → ")
print("Step 2", end=" → ")
print("Done!")
# Step 1 → Step 2 → Done!
Printing to a File
Use the file parameter to write output to a file instead of the console:
with open("output.txt", "w") as f:
print("Hello, File!", file=f)
print("Line 2", file=f)
This creates output.txt with:
Hello, File!
Line 2
Pretty Printing with pprint
For complex data structures, use pprint for readable output:
from pprint import pprint
data = {"name": "Alice", "scores": [95, 87, 92], "address": {"city": "NYC", "zip": "10001"}}
# Regular print — hard to read
print(data)
# {'name': 'Alice', 'scores': [95, 87, 92], 'address': {'city': 'NYC', 'zip': '10001'}}
# Pretty print — formatted
pprint(data)
# {'address': {'city': 'NYC', 'zip': '10001'},
# 'name': 'Alice',
# 'scores': [95, 87, 92]}
print() Function Signature
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
| Parameter | Default | Description |
|---|---|---|
*objects |
— | Values to print (any number) |
sep |
' ' |
Separator between values |
end |
'\n' |
String appended after last value |
file |
sys.stdout |
Output destination |
flush |
False |
Force flush the output buffer |
The flush Parameter
Useful for progress indicators where you want immediate output:
import time
for i in range(5):
print(f"\rProgress: {i+1}/5", end="", flush=True)
time.sleep(1)
print() # Final newline
Common Patterns
# Blank line
print()
# Separator line
print("-" * 50)
# Formatted table row
print(f"{'Name':<15} {'Age':>5} {'City':<10}")
print(f"{'Alice':<15} {25:>5} {'NYC':<10}")
# Name Age City
# Alice 25 NYC
Summary
print()displays output to the console- Use
septo change the separator (default: space) - Use
endto change the ending character (default: newline) - Use
fileto redirect output to a file - Use
pprintfor readable complex data structures - Use
flush=Truefor immediate output (progress bars)
Next up: input() Function →