Skip to content

Python print() Function: Parameters, f-strings, and Output Control

print() is a built-in function that writes to sys.stdout by default. It is the standard tool for quick console output and debugging in scripts. For application code, prefer the logging module — it adds levels, timestamps, and routing to files without changing your code.

Terminal window
# Save as print_examples.py and run with:
python print_examples.py
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Parameter Default Purpose
*objects — One or more values to print
sep ' ' Separator between multiple values
end '\n' Appended after the last value
file sys.stdout Output stream
flush False Flush the stream immediately if True
print("Hello, world!")
# Hello, world!
print(42)
print(3.14)
print(True)
print(None)
# 42
# 3.14
# True
# None
# Multiple values — separated by a space
print("Order", "ORD-001", "confirmed")
# Order ORD-001 confirmed
# Default separator is a space
print("2026", "11", "13", sep="-")
# 2026-11-13
print("ORD", "001", sep="")
# ORD001
# CSV-style output
print("ORD-001", "BensonCo", "149.99", sep=",")
# ORD-001,BensonCo,149.99
print("a", "b", "c", sep=" | ")
# a | b | c
# Default end is a newline \n
print("Processing...", end=" ")
print("Done.")
# Processing... Done.
# Progress display — overwrite the same line
import time
for i in range(1, 4):
print(f"\rProcessing order {i}/3", end="", flush=True)
time.sleep(0.5)
print() # final newline

f-strings: the preferred way to format output

Section titled “f-strings: the preferred way to format output”

f-strings (Python 3.6+) are the standard for string interpolation:

order_id = "ORD-001"
customer = "BensonCo"
amount = 149.99
print(f"Order {order_id} for {customer}: ${amount:.2f}")
# Order ORD-001 for BensonCo: $149.99
# Alignment
print(f"{'Item':<20} {'Qty':>5} {'Price':>10}")
print(f"{'Widget':<20} {12:>5} {29.99:>10.2f}")
# Item Qty Price
# Widget 12 29.99
# Debug: f-string with = prints the expression and its value
x = 42
print(f"{x=}") # x=42
print(f"{x*2=}") # x*2=84
# Thousands separator
total = 1_234_567.89
print(f"{total:,.2f}") # 1,234,567.89
# Percentage
ratio = 0.837
print(f"{ratio:.1%}") # 83.7%
import sys
# Write to stderr (shown in red in most terminals)
print("Error: order not found", file=sys.stderr)
# Write to a file
with open("output.log", "w", encoding="utf-8") as f:
print("ORD-001 processed", file=f)
print("ORD-002 skipped", file=f)

Normally, Python buffers writes. flush=True forces the buffer to be written immediately — important for real-time progress bars or when the output is piped:

import time
import sys
for i in range(5):
print(f"Step {i+1}/5", end="\r", flush=True)
time.sleep(0.3)
print()
orders = ["ORD-001", "ORD-002", "ORD-003"]
print(orders) # ['ORD-001', 'ORD-002', 'ORD-003']
print(*orders) # ORD-001 ORD-002 ORD-003 (unpacked)
print(*orders, sep="\n") # one per line
order = {"id": "ORD-001", "amount": 149.99}
print(order) # {'id': 'ORD-001', 'amount': 149.99}
import json
print(json.dumps(order, indent=2)) # pretty-printed JSON

print() is fine for scripts and quick debugging. For application code that runs in production, use the logging module:

import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s — %(message)s",
)
logger = logging.getLogger(__name__)
logger.info("Order %s confirmed", "ORD-001")
logger.warning("Retry %d for order %s", 2, "ORD-002")
logger.error("Payment failed for order %s", "ORD-003")

logging gives you levels (DEBUG, INFO, WARNING, ERROR, CRITICAL), timestamps, source module names, and the ability to route output to files, remote systems, or structured JSON — none of which print() supports.

  • Using print() in production application code. It goes to stdout without timestamps, levels, or routing. Use logging.
  • Not flushing in long-running scripts. If stdout is redirected to a file, unbuffered output may not appear until the script ends. Use flush=True or run Python with -u (unbuffered).
  • Formatting numbers with string concatenation. "Total: " + str(amount) is harder to read than f"Total: {amount:.2f}".
  • sep controls the separator between values; end controls what follows the last value.
  • f-strings are the standard for string interpolation; {value:.2f} controls decimal places.
  • Use file=sys.stderr to write error output to stderr.
  • Use flush=True for real-time progress output.
  • Use logging instead of print() in any code that runs unattended.