Print without a newline in Python

Control print endings with end, separate arguments with sep, and flush progress output when it would otherwise remain buffered.

Best practices
Python programming and application logging

Set end="" to prevent print() from adding a newline. The default is end="\n"; you can replace it with a space, a comma, or another string.

Continue on the same line

print("Hello", end="")
print(" world")
# Hello world

The second call uses the default ending, so the complete line ends with a newline.

For repeated output:

for number in range(5):
    print(number, end=" ")
print()

This leaves a trailing space. Using end=", " similarly leaves a trailing comma and space. If you want separators only between values, use one call instead:

print(*range(5), sep=", ")  # 0, 1, 2, 3, 4

Distinguish sep from end

print("a", "b", "c")            # a b c
print("a", "b", "c", sep="-")   # a-b-c
print("a", "b", "c", sep="")    # abc

sep goes between the arguments in one call. end is added after the final argument. They control different parts of the output.

Update a progress line

import time

for step in range(10):
    print(f"\rProgress: {(step + 1) * 10:3d}%", end="", flush=True)
    time.sleep(0.03)
print()

On a terminal, \r returns the cursor to the start of the current line. The fixed-width percentage prevents leftover characters when updating it. flush=True flushes Python's stream buffer so a missing newline does not delay the update. It does not guarantee that every downstream collector displays the text immediately.

Redirected output may contain literal carriage returns rather than an animated line. Prefer ordinary newline-separated events for persistent logs.

FAQ

Why has nothing appeared yet? Output may be buffered, particularly when redirected to a file or pipe. Flush explicitly for incremental output, or use Python's -u option when unbuffered standard streams are appropriate.

What about Python 2's trailing comma? That syntax belongs to the obsolete Python 2 print statement. In Python 3, use print(..., end="").

References

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting