How can I print multiple things on the same line, one at a time?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Printing multiple items on the same line incrementally is a common requirement for progress indicators, dynamic status updates, and formatted console output. Most languages append a newline after each print call by default, so you need specific techniques to suppress it. This article covers the primary approaches in Python and briefly touches on how other languages handle the same task.
Using print() with end Parameter
In Python 3, the print() function accepts an end parameter that controls what character is appended after the output. By default it is '\n' (newline). Setting it to an empty string or a space keeps subsequent output on the same line.
Note that you typically want a final print() call at the end to move the cursor to the next line when you are done.
Using sys.stdout.write()
The sys.stdout.write() method gives you lower-level control over output. Unlike print(), it does not append any trailing character automatically, making it ideal for precise formatting.
One important difference is that sys.stdout.write() only accepts strings, so you must convert other types explicitly with str().
Flushing Output for Real-Time Display
By default, Python buffers stdout when writing to a terminal. This means output may not appear immediately, which is problematic for progress bars and real-time status updates. Use flush=True in print() or call sys.stdout.flush() explicitly.
Without flushing, the entire output might appear all at once when the buffer fills or the program exits.
Overwriting Lines with Carriage Return
The carriage return character \r moves the cursor back to the beginning of the current line without advancing to a new line. This lets you overwrite previous output, which is useful for progress counters and spinners.
When overwriting, make sure the new output is at least as long as the previous output, or pad with spaces to avoid leftover characters.
Using f-strings for Formatted Same-Line Output
Python f-strings combine naturally with the end parameter to produce clean, formatted same-line output.
Same-Line Printing in Other Languages
The concept applies across languages, though the syntax varies.
In each case, the key is using the variant of the print function that does not automatically append a newline.
Common Pitfalls
- Forgetting to flush: Without
flush=Trueorsys.stdout.flush(), buffered output may not appear until the program ends, making progress indicators useless. - Leftover characters when overwriting: Using
\rto overwrite a longer line with a shorter one leaves trailing characters from the previous output, so always pad with spaces. - Missing the final newline: After printing on the same line, forgetting a trailing
print()causes the shell prompt to appear on the same line as your output. - Using
print()in Python 2 syntax: In Python 2,printis a statement, not a function. Writingprint("text", end='')in Python 2 prints a tuple instead. Usefrom __future__ import print_functionfor compatibility. - Mixing
print()andsys.stdout.write(): These two methods may buffer independently, causing output to appear in an unexpected order. Stick to one approach within a single output sequence.
Summary
- Use
print(value, end='')in Python 3 to suppress the default newline and keep output on the same line. - Use
sys.stdout.write()for lower-level control when you want no automatic formatting at all. - Always pass
flush=Trueor callsys.stdout.flush()when building real-time progress indicators. - Use the carriage return
\rto overwrite the current line for counters, spinners, and progress bars. - In other languages, use the non-newline print variant such as
process.stdout.write()in Node.js,System.out.print()in Java, orprintf()in C.

