Python Print Without Newline: Using end, sep and flush

Print without a newline in Python using end, the difference between end and sep, printing a list on one line, progress lines with \r and flush.

Python

To print without a newline in Python, we pass end=”” (or any other string) to print(), which replaces the newline that print() adds after its output by default. Progress messages, prompts and values printed in a loop on one line all use it.

print("Loading", end="")
print("...done")          # Loading...done

We also look at the difference between end and sep, printing a list on one line, progress updates with \r and flush, and sys.stdout.write(). The examples use Python 3.14.

1. The end Parameter

The print() function writes its arguments followed by the value of end, and the default value is “\n”. Any string works, such as an empty string or a space.

for n in [1, 2, 3]:
    print(n, end=" ")
print()                   # ends the line
1 2 3 

The final print() without arguments writes only the default newline, so the next output starts on a new line.

2. end vs sep

The sep parameter goes between the arguments of one call, and end goes after the last one. Mixing them up is the usual reason for missing or extra spaces.

print("a", "b", "c", sep="-", end="!\n")    # a-b-c!
print("a", "b", "c", sep="")               # abc

3. Printing a List on One Line

Unpacking a list with * passes each element as a separate argument, so sep controls the gaps. The join() method builds the same line as a string, but it needs string elements.

scores = [90, 85, 77]
print(*scores, sep=", ")                       # 90, 85, 77
line = ", ".join(str(s) for s in scores)       # '90, 85, 77'

4. Updating a Progress Line with \r and flush

The carriage return \r moves the cursor to the start of the line, so the next print overwrites the previous one. Output without a newline can stay in the buffer, so we pass flush=True to show each update right away.

import time

for pct in range(0, 101, 25):
    print(f"\rProgress: {pct}%", end="", flush=True)
    time.sleep (0.2)
print()
Progress: 100%

When a new message is shorter than the old one, leftover characters remain on screen, so we pad the text to a fixed width, for example print(f”\r{msg}”.ljust(30), end=””). Running Python with python -u also turns off output buffering for the whole script.

5. sys.stdout.write()

The sys.stdout.write() method writes a string as given, with no separator and no newline. It accepts only a str, and it returns the number of characters written, which the interactive prompt echoes.

import sys

count = sys.stdout.write("no newline here")    # count = 15
sys.stdout.write("\n")

Python 2 code ended a print statement with a comma, which printed a space instead of the newline. In Python 3, print is a function, and end=” “ replaces that trick.

6. A Spinner That Updates in Place

A spinner shows that a long task is still running. Each frame overwrites the previous one with \r, stays on the line with end=”” and appears at once with flush=True.

import itertools
import time

frames = itertools.cycle("|/-\\")
for _ in range(8):
    print(f"\rWorking {next(frames)}", end="", flush=True)
    time.sleep (0.1)
print("\rWorking done")
Working done

7. Same-Line Output Questions

Same-line output also matters for files, and buffering decides when the text appears.

7.1. Does end work when printing to a file?

Yes. The end and file parameters combine, so print(“a”, end=””, file=f) writes to the file without a newline.

7.2. Why does output with end=”” appear late or all at once?

The standard output is buffered, so text without a newline can wait in memory. Passing flush=True, running python -u or setting the environment variable PYTHONUNBUFFERED=1 sends it right away.

7.3. How do we print without a newline in Python 2?

Python 2 code can add from __future__ import print_function at the top and use end=”” like Python 3. Python 2 is no longer supported, so new code targets Python 3.

8. Conclusion

Passing end=”” keeps the next output on the same line, and sep handles the gaps inside one call. For live progress we combine \r, end=”” and flush=True. More options of the function are in Python print().

9. References

Happy Learning !!

Source Code on Github

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.