Python Tutorial

Python print()

Clear guide to using python print(): syntax, parameters (sep, end, file, flush), examples for console and file output, and formatting tips.

Drake Nguyen

Founder · System Architect

3 min read
Python print()
Python print()

Introduction to python print()

The python print() function is the simplest way to send text and values to the console or another file-like object. It’s used frequently for debugging, logging simple messages, or producing human-readable output. This guide explains the print function syntax, common parameters (sep, end, file, flush), and practical examples such as printing multiple variables, controlling separators, and writing output to a file.

Basic syntax

At its core, the print function accepts one or more values and converts them to text before sending them to standard output (stdout) or another target. The general form is:

print(*values, sep=' ', end='\n', file=sys.stdout, flush=False)

Key parameters to remember: sep controls how multiple values are joined, end determines what is appended after the final value (default newline), file directs output to a file-like object, and flush forces the output buffer to be written immediately.

Printing multiple values

print() accepts multiple arguments separated by commas—each argument is converted to a string and joined using the sep parameter.

# print multiple variables to console
a = 10
b = 'items'
c = 3.14
print(a, b, c)
# Output: 10 items 3.14

Custom separator (python print sep)

Use the sep keyword to change the delimiter between printed values. This is helpful when you want an underscore or another character between items.

print(a, b, c, sep='_')
# Output: 10_items_3.14

Suppressing newline (python print end)

By default print() appends a newline. Use the end parameter to change or remove that trailing character. This is useful for printing items on the same line or customizing line endings.

words = ['alpha', 'beta', 'gamma']
for w in words:
    print(w, end='-')
# Output: alpha-beta-gamma-

Redirecting output: python print to file

The file parameter sends the output to any object that implements a write() method, such as a file opened with open(). This is an easy way to produce a text file from script output.

with open('output.txt', 'w') as f:
    print('Header', file=f)
    for item in words:
        print(item, file=f)
# Check output.txt for the written lines

Print to stderr

You can send messages to standard error (stderr) instead of stdout—useful for logging errors or warnings.

import sys
print('This is an error message', file=sys.stderr)

Buffered output and flush

The flush parameter forces the OS buffer to write immediately. Set flush=True when you need prompt output (for example, in long-running loops or when following live progress in tests).

import time
for i in range(3):
    print(i, end=' ', flush=True)
    time.sleep(1)

Formatting vs print()

print() simply converts values to strings and joins them. For more precise formatting, prefer f-strings or str.format(). Combining print() with formatted strings gives cleaner, localized, and predictable output.

# f-string example
name = 'Alice'
score = 95.5
print(f'{name} scored {score:.1f} points')
# Output: Alice scored 95.5 points

Practical tips and common patterns

  • To print list items inside a loop use print(item) or, for a single-line display, join with a separator: print(', '.join(map(str, my_list))).
  • For printing many values with a custom separator: print(*values, sep='_') prints multiple values separated by underscores (python print multiple values separated by underscore).
  • To append output to a file use open('output.txt', 'a') instead of 'w' to avoid overwriting.
  • When debugging, send diagnostics to stderr so they don’t mix with normal program output: print('debug', file=sys.stderr).

Summary

The python print() function is flexible: control separators with sep, change line endings with end, write to files with file, and force immediate output with flush. Use print() for quick console output and f-strings or formatting functions when you need structured, precise output.

Tip: prefer f-strings for formatted output and use print() parameters to control how and where the final text appears.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.