Python print() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, print(*objects) converts each object with str(), writes them to the console separated by a space, and finishes with a newline. print() with no arguments prints an empty line.

Four keyword arguments control the output: sep (the separator, default " "), end (what comes last, default "\n"), file (where to write, default sys.stdout), and flush (force the text out immediately).

print() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass any number of values, then optional keywords.

python
print(value1, value2, sep=" ", end="\n", file=sys.stdout, flush=False)
CallWhat it does
print("a", "b")Prints a b.
print("a", "b", sep="-")Prints a-b.
print("no newline", end="")Leaves the cursor on the same line.
print(*items)Prints the items of a list separated by spaces.

print() returns None; it is for showing output, not for building strings. Use an f-string or "".join() when you need the text itself.

Library declarations
python
print(*objects, sep=" ", end="\n", file=None, flush=False)
# file=None means sys.stdout

Parameters

ParameterDescription
*objectsZero or more values to print. Each is converted with str().
sepOptional. String placed between values. Default one space.
endOptional. String written after the last value. Default "\n".
fileOptional. An object with a write() method, such as sys.stderr, an open file, or io.StringIO.
flushOptional. True forces the stream to write out immediately.

Return Value

Return ValueDescription
Noneprint() only writes output.

Python Examples

Example 1 — Control Separators and Line Endings

Change the separator, print without a newline, and add a blank line.

python
print("a", "b", "c")
print("a", "b", "c", sep="-")
print("2024", "03", "09", sep="/")
print("Loading", end="")
print("...", end=" ")
print("done")
print()
print("after a blank line")

How It Works

  1. Default spacing. Without keywords, values are joined with one space and the line ends with a newline.
  2. Custom sep. sep="-" and sep="/" replace the space between values.
  3. Staying on one line. end="" and end=" " stop the newline, so three calls build Loading... done.
  4. Empty print. print() writes just the newline, producing a blank line.

Example 2 — Print Lists, Unpacked Items, and Floats

Compare printing a list with unpacking it, see the classic float output, and align text with an f-string.

python
values = [1, 2.5, "text", None, True, [1, 2], {"k": "v"}]

print(*values)
print(values)
print(0.1 + 0.2)
print(*range(5), sep=", ")
print(f"{'left':<6}|{'right':>6}|")

How It Works

  1. Unpacking with *. print(*values) passes each item separately, so strings print without quotes.
  2. Whole list. print(values) prints the list's repr, with quotes around 'text'.
  3. Float precision. 0.1 + 0.2 is slightly above 0.3 in binary, and print() shows that exactly.
  4. Ranges and alignment. *range(5) with sep=", " makes a list-like line, and the f-string pads to fixed widths.

Example 3 — Print to stderr, a Buffer, and Flush

Send text to an in-memory buffer, write a warning to standard error, and flush a progress line.

python
import io
import sys

buffer = io.StringIO()
print("first line", file=buffer)
print("second", "line", sep="_", file=buffer)
print(repr(buffer.getvalue()))

print("Warning: low disk space", file=sys.stderr)
print("Progress: 100%", flush=True)

How It Works

  1. Any writable stream. file=buffer sends the text into an io.StringIO object instead of the console.
  2. Captured text. getvalue() returns everything written, including both newlines added by print().
  3. Standard error. file=sys.stderr writes the warning to the error stream, which stays visible even when stdout is redirected to a file.
  4. flush=True. The progress line is pushed out immediately instead of waiting in the output buffer.

Frequently Asked Questions

Pass end="": print("Loading", end=""). The next print continues on the same line.
Use print(*items, sep=", "). For text you can also use print(", ".join(map(str, items))).
Import sys and call print("message", file=sys.stderr).
Floats are binary approximations, and print shows the exact stored value. Use f"{x:.2f}" or round(x, 2) for display.
It forces buffered output to be written immediately, which is useful for progress messages and when output is piped to another program.

Next: property()

Turn methods into a managed attribute.

property() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful