Python
printing
programming
coding tips
standard output

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="".

python
print("Hello", end="")
print("World")

That produces:

text
HelloWorld

If the problem is the automatic space between multiple arguments, use sep="":

python
print("Hello", "World", 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:

python
print("A", "B", "C", sep="", end="")
print("123")

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.

python
1import sys
2
3sys.stdout.write("Hello")
4sys.stdout.write("World")
5sys.stdout.write("\n")

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.

python
1import time
2
3for i in range(3):
4    print(".", end="", flush=True)
5    time.sleep(1)
6
7print()

You can do the same thing with sys.stdout:

python
1import sys
2import time
3
4for i in range(3):
5    sys.stdout.write("#")
6    sys.stdout.flush()
7    time.sleep(1)
8
9sys.stdout.write("\n")

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.

python
1name = "Ada"
2score = 42
3
4print(f"{name}:{score}")

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 stop print() from adding a newline.
  • Use sep="" to stop print() 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.

Course illustration
Course illustration

All Rights Reserved.