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:
sepcontrols the separator between valuesendcontrols 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.
Syntax and Examples
Core syntax
print(*objects, sep=' ', end='\n')
Default values:
sep=' 'means values are separated by a spaceend='\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:
Step by Step Execution
Consider this code:
for i in range(4):
print('.', end='')
Step-by-step trace
range(4)produces the values0, 1, 2, 3- The loop runs 4 times
- On each iteration,
print('.', end='')is called print()outputs.- Because
end='', no newline is added - 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:
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)
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()
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 and
Comparisons
| Approach | Adds spaces between values? | Adds newline at end? | Best for |
|---|---|---|---|
print('a', 'b') | Yes | Yes | Normal readable output |
print('a', 'b', sep='') | No | Yes | Joining multiple values without spaces |
print('a', end='') | Not relevant | No | Repeated output on one line |
print('a', 'b', sep='', end='') | No | No | Full control with print() |
sys.stdout.write('ab') | No automatic formatting |
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
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?
sepis placed between multiple printed valuesendis placed after the final value
Is sys.stdout.write() better than print()?
Not always. print() is simpler for most tasks. Use when you need exact stream output and do not want automatic formatting.
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
- Print the text
Loadingwithout ending the line. - Print five dots on the same line using a loop.
- Do not allow spaces between the dots.
- Move to a new line after the dots are finished.
- Print
Done!on its own line.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.