Python foundations

Python print(): sep, end, file, and flush

Control Python print() spacing, newlines, output files, and buffering. See why a printed value and a returned value are different.

Python’s print() writes text to standard output by default. It separates multiple values with spaces and ends with a newline. Set sep to change the separator, end to change what follows the last value, and file to change where the text goes. The call returns None.

item = "notebook"
count = 3
print(item, count)
print(item, count, sep=" = ")
print("Done", end=".")

The first line is notebook 3. The second is notebook = 3. The final call writes Done. without a newline, so a terminal prompt may appear immediately after the period.

Change the separator with sep and the line ending with end

sep goes between arguments. If there is just one argument, there is no boundary at which to use it. end comes after the last argument even when the argument list is empty. For example, print() prints a blank line, while print(end="") prints nothing.

Use print("Status", "ready", sep=": ") for a few items with a consistent separator. Use an f-string when a field needs its own format, such as fixed decimal precision or width.

Keep results and diagnostics distinct

In a command-line program, standard output can be a human report or a stream another program consumes. If that stream has to contain valid JSON, a debugging line can break the caller. Send diagnostic messages to standard error and keep the standard output contract clear.

from io import StringIO

buffer = StringIO()
print("room", "ready", sep=": ", file=buffer)
assert buffer.getvalue() == "room: ready\n"

The file argument accepts a text stream such as an open text file or StringIO. For real error output, use file=sys.stderr after importing sys. For recurring diagnostics with levels and timestamps, the logging library is usually a better home once a script grows beyond a few simple messages.

Check why a message appears late

Streams can buffer text before showing it or writing it elsewhere. By default, behavior depends on the stream and runtime. If a short progress message must be visible immediately, print("Working…", end="", flush=True) asks that stream to flush. It does not guarantee what an application reading the stream will do with the bytes.

Flushing every line in a large loop can slow output. Prefer occasional progress updates where the reader benefits from knowing the program is alive. For redirected output, the destination might be a file or build log where an interactive progress indicator has little purpose.

Verify exactly what the reader receives

Test content and line endings when other programs depend on them. StringIO is a simple way to inspect text in memory. If behavior differs in a shell, check where the streams were redirected and whether the caller reads standard output or error. A printed value may look correct during manual testing while still being unusable as a return value. Verify the interface the caller actually uses.

Sources and further reading

Related reading

Browse Python foundations · Explore project tutorials

Search PythonProjects.org