How to print without a newline or space
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, print() adds a newline after each call and inserts spaces between multiple arguments unless you tell it not to. When you need exact output formatting, the usual fix is to change the end or sep argument, or to write directly to sys.stdout.
This matters in command-line tools, progress indicators, and coding exercises where the output must match exactly. The key is knowing whether you are trying to remove the trailing newline, the separator between values, or both.
Control print() with end and sep
By default, print("A") ends with a newline. To keep the cursor on the same line, set end="".
That produces:
If the problem is the automatic space between multiple arguments, use sep="":
This solves a different issue. end controls what comes after the printed text. sep controls what goes between printed arguments.
You can combine them:
That is usually the simplest answer when the output still fits naturally inside print().
Use sys.stdout.write() for Full Control
If you want to avoid print() defaults entirely, write directly to standard output.
This approach is useful when you are generating output incrementally or when you want complete control over exactly what is written. It is also a good reminder that print() is a convenience function, not the only output API available.
Direct writing is common in interactive terminal programs where the output format is tightly controlled.
Another practical case is building prompts that should stay on one line while waiting for user input. In that situation, removing the automatic newline is not just cosmetic. It changes how the terminal interaction feels.
Flush Output When Timing Matters
Sometimes the code is correct but the output does not appear immediately because of buffering. In that case, flush the stream.
You can do the same thing with sys.stdout:
This is useful for status displays and progress meters where delayed output makes the program look stuck.
Prefer Formatting When the Output Shape Is Fixed
If the final output has one known format, a single formatted string is often clearer than several partial print() calls.
That avoids both extra spaces and extra newlines without relying on multiple output calls. end and sep are still valuable, but string formatting is often simpler when the output is already known as one unit.
Common Pitfalls
The biggest mistake is confusing end and sep. end removes or changes the trailing newline, while sep changes the separator between arguments.
Another common issue is forgetting about buffering. A loop can be printing correctly and still appear silent until you flush the output.
It is also easy to overuse multiple print() calls when one formatted string would be easier to read and maintain.
Finally, sys.stdout.write() expects a string. If you need to output numbers or other values directly, convert or format them first.
Summary
- Use
end=""to stopprint()from adding a newline. - Use
sep=""to stopprint()from inserting spaces between arguments. - Use
sys.stdout.write()when you want lower-level control. - Flush output explicitly when immediate display matters.
- Prefer one formatted string when the output shape is fixed and simple.

