print() is the most common way to see what's happening inside a Python program. It accepts any number of positional arguments, converts each one with str(), joins them using the sep keyword (a single space by default), and finishes the line with the end keyword (a newline by default). The file argument redirects output elsewhere (like a log file), and flush forces the buffer to write immediately instead of waiting.
1Understanding print()
print() is the most common way to see what's happening inside a Python program. It accepts any number of positional arguments, converts each one with str(), joins them using the sep keyword (a single space by default), and finishes the line with the end keyword (a newline by default). The file argument redirects output elsewhere (like a log file), and flush forces the buffer to write immediately instead of waiting.
Use f-strings (f"...") to build the message instead of passing many comma-separated arguments — it's easier to read and avoids surprises from the default space separator.
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")2Practical Example
Here is a real-world application of print() showing how it is used in production Python code.
import time
for i in range(5):
print(f"Loading... {i * 20}%")
time.sleep(0.3)
print("Done!")3Best Practices
Follow these guidelines when working with print():
1. Prefer f-strings for formatting values instead of string concatenation with +
2. Set end='' when you need to build a line across multiple print() calls, e.g. a progress indicator
3. Use the logging module instead of print() for anything beyond quick, throwaway debugging
Tip: Use f-strings (f"...") to build the message instead of passing many comma-separated arguments — it's easier to read and avoids surprises from the default space separator.
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")