Question

How to Print Without a Newline or Space in Python

pythontrailing-newline

Question

In Python, print() often adds extra characters to the output:

for i in range(4):
    print('.')

Output:

.
.
.
.

And when printing multiple values in one call:

print('.', '.', '.', '.')

Output:

. . . .

In the first example, print() adds a newline after each call. In the second, it adds spaces between the values. How can you print .... instead, without automatic newlines or spaces? More generally, how can you append text to standard output in Python?

Short Answer

By the end of this page, you will understand why Python's print() adds newlines and spaces by default, how to change that behavior with the end and sep arguments, and when to use sys.stdout.write() for lower-level output control.

Concept

print() is a convenient high-level function for writing text to the console. Its default behavior is designed to make output readable:

  • it adds a space between multiple values
  • it adds a newline at the end of each call

That is why these two examples behave differently:

print('.', '.', '.')   # spaces between values
print('.')             # newline at the end

Python lets you override both defaults:

  • sep controls the separator between values
  • end controls what is written after the last value

This matters because many programs need precise output formatting, such as:

  • progress indicators
  • command-line tools
  • generated text files
  • logging and status messages
  • ASCII art and text-based UI output

If you need even more control, you can write directly to the output stream with sys.stdout.write(). That avoids print()'s automatic formatting entirely.

Mental Model

Think of print() like a helpful assistant who always formats your message before sending it:

  • if you give it multiple items, it puts a space between them
  • when it finishes, it presses Enter and starts a new line

Usually that is helpful. But sometimes you want exact control. In that case, you tell the assistant:

  • sep='' → "do not put anything between items"
  • end='' → "do not press Enter at the end"

If you want no help at all, use sys.stdout.write(), which is like writing directly onto the screen yourself.

Take Quiz

Syntax and Examples

Core syntax

print(*objects, sep=' ', end='\n')

Default values:

  • sep=' ' means values are separated by a space
  • end='\n' means a newline is added at the end

Print without a newline

for i in range(4):
    print('.', end='')

Output:

....

Here, each call to print() ends with an empty string instead of \n.

Print multiple values without spaces

print('.', '.', '.', '.', sep='')

Output:

....

Here, the separator between values is an empty string.

Remove both spaces and newline

print('A', 'B', 'C', sep='', end='')

Output:

ABC

Using sys.stdout.write()

import sys

for i in range(4):
    sys.stdout.write('.')

Output:

....

sys.stdout.write() writes exactly the string you provide. It does not automatically add spaces or newlines.

A clean beginner-friendly example

for i in range(4):
    print('.', end='')
print()  # move to the next line afterward

This prints .... and then moves the cursor to a new line at the end.

Step by Step Execution

Consider this code:

for i in range(4):
    print('.', end='')

Step-by-step trace

  1. range(4) produces the values 0, 1, 2, 3
  2. The loop runs 4 times
  3. On each iteration, print('.', end='') is called
  4. print() outputs .
  5. Because end='', no newline is added
  6. The next . is printed immediately after the previous one

Output growth

  • after first iteration: .
  • after second iteration: ..
  • after third iteration: ...
  • after fourth iteration: ....

Final output:

....

Now compare with this:

print('.', '.', '.', '.', sep='')

What happens here

  1. print() receives 4 arguments
  2. Normally it would join them with spaces
  3. But sep='' changes the separator to an empty string
  4. So the values are joined as ....
  5. print() still adds its default newline at the end

So the visible output is ...., followed by moving to the next line.

Real World Use Cases

This kind of output control is useful in many practical situations:

Progress indicators

for i in range(10):
    print('.', end='')

Used to show that work is happening.

Building one-line status messages

print('Loading', end='')
print('...', end='')
print(' done')

Text-based UI elements

for i in range(20):
    print('#', end='')
print()

Useful for progress bars or separators.

Writing formatted reports to standard output

name = 'Alice'
score = 95
print('Name:', name, '|', 'Score:', score)

If formatting must be exact, sep and end help avoid unwanted spaces.

Generating machine-readable output

Sometimes scripts are consumed by other tools, so output must be exact. Extra spaces or newlines can break parsing.

Take Quiz

Real Codebase Usage

In real projects, developers often use these patterns:

1. Customizing print() for CLI output

print('Processing...', end='')
# do work
print('done')

This keeps status text on one line.

2. Printing joined values

Instead of many separate print calls, developers often build the full string first:

items = ['.', '.', '.', '.']
print(''.join(items))

This is common when output comes from a list.

3. Guarding output format in scripts

When exact output matters, developers avoid relying on defaults:

print(result, end='')

That makes behavior explicit and easier to review.

4. Using sys.stdout.write() in lower-level code

import sys
sys.stdout.write('Hello')
sys.stdout.flush()

This is useful when you want stream-like behavior or immediate output.

5. Flushing output for live updates

print('.', end='', flush=True)

Without flushing, some environments may delay showing output until later.

6. Prefer string construction for complex output

In larger codebases, developers often build strings using f-strings, join(), or templates, then print once. That reduces formatting mistakes and makes testing easier.

Common Mistakes

1. Forgetting that print() adds a newline

Broken expectation:

for i in range(4):
    print('.')

This prints each dot on a new line.

Fix:

for i in range(4):
    print('.', end='')

2. Forgetting that multiple arguments are separated by spaces

Broken expectation:

print('.', '.', '.')

Output:

. . .

Fix:

print('.', '.', '.', sep='')

3. Mixing up sep and end

  • sep is between arguments
  • end is after the whole print call

Example:

print('A', 'B', sep='-', end='!')

Output:

A-B!

4. Expecting immediate output without flushing

print('.', end='')

In some environments, the dot may not appear immediately.

Fix:

print('.', end='', flush=True)

5. Using sys.stdout.write() with non-strings

Broken code:

import sys
sys.stdout.write(123)

sys.stdout.write() expects a string.

Fix:

import sys
sys.stdout.write(str(123))

6. Forgetting to move to a new line later

If you use end='', the next output may appear on the same line unexpectedly.

for i in range(4):
    print('.', end='')
print('done')

Output:

....done

If you want done on the next line:

for i in range(4):
    print('.', end='')
print()
print('done')

Comparisons

ApproachAdds spaces between values?Adds newline at end?Best for
print('a', 'b')YesYesNormal readable output
print('a', 'b', sep='')NoYesJoining multiple values without spaces
print('a', end='')Not relevantNoRepeated output on one line
print('a', 'b', sep='', end='')NoNoFull control with print()
sys.stdout.write('ab')No automatic formattingNoLow-level stream writing
''.join(list_of_strings) + print()No, if joined with ''Yes by defaultBuilding output from many strings

print() vs sys.stdout.write()

Featureprint()sys.stdout.write()
Easy to useYesLess convenient
Auto converts objects to textYesUsually no
Adds separator automaticallyYesNo
Adds newline automaticallyYesNo
Good for quick scriptsYesYes, but more manual
Good for exact stream controlSometimesYes

In most beginner cases, print(..., end='') or print(..., sep='') is the simplest solution.

Take Quiz

Cheat Sheet

Quick reference

Default print() behavior

print('a', 'b')
# sep=' ', end='\n'

No newline

print('text', end='')

No spaces between multiple values

print('a', 'b', 'c', sep='')

No spaces and no newline

print('a', 'b', sep='', end='')

Repeat output on one line

for i in range(4):
    print('.', end='')
print()

Write directly to stdout

import sys
sys.stdout.write('hello')

Flush immediately

print('.', end='', flush=True)

Rules to remember

  • sep affects text between arguments
  • end affects text after the print call
  • print() is higher-level and easier
  • sys.stdout.write() gives exact output control
  • sys.stdout.write() expects a string

Common patterns

print(*items, sep='')
print(message, end='')
print(''.join(parts))

FAQ

How do I print on the same line in Python?

Use end='':

print('Hello', end='')
print('World')

This outputs HelloWorld.

How do I remove spaces between values in print()?

Use sep='':

print('a', 'b', 'c', sep='')

What is the difference between sep and end in Python?

  • sep is placed between multiple printed values
  • end is placed after the final value

Is sys.stdout.write() better than print()?

Not always. print() is simpler for most tasks. Use sys.stdout.write() when you need exact stream output and do not want automatic formatting.

Why is my output not appearing immediately?

Output may be buffered. Use:

print('.', end='', flush=True)

Can I print many strings without spaces using a list?

Yes:

parts = ['a', 'b', 'c']
print(''.join(parts))

Does print() automatically convert numbers to strings?

Yes:

print(1, 2, 3, sep='')

But sys.stdout.write() does not automatically do that reliably, so convert manually with str().

Take Quiz

Mini Project

Description

Build a tiny terminal progress display that prints dots on one line while work is happening. This demonstrates how to control print() output using end='' and how to finish neatly with a newline.

Goal

Create a script that prints a one-line loading message followed by dots, then prints Done! on the next line.

Requirements

  1. Print the text Loading without ending the line.
  2. Print five dots on the same line using a loop.
  3. Do not allow spaces between the dots.
  4. Move to a new line after the dots are finished.
  5. Print Done! on its own line.
Take Quiz

Keep learning

Related questions

View more