Python
sys.stdout.write
print function
programming
code comparison

The difference between sys.stdout.write and print?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

print is Python's convenience function for human-friendly output, while sys.stdout.write is a lower-level stream method. The biggest differences are automatic formatting, newline behavior, and what kinds of values each one accepts directly.

print converts objects to text, inserts separators between arguments, and appends a newline by default.

python
1name = "Ava"
2age = 31
3
4print("User:", name, "Age:", age)

Output:

text
User: Ava Age: 31

This is why print is the right choice for debugging, scripts, and most command-line output.

sys.stdout.write writes raw text to the stream

sys.stdout.write is a method on the standard output stream. It writes exactly the string you pass to it and returns the number of characters written.

python
1import sys
2
3count = sys.stdout.write("Hello")
4sys.stdout.write("\n")
5print(f"wrote {count} characters")

Unlike print, it does not automatically add spaces or a newline. It also expects a string, so passing an integer directly is an error.

Formatting behavior is the main practical difference

Compare the two directly:

python
1import sys
2
3print("A", "B", "C")
4sys.stdout.write("A")
5sys.stdout.write("B")
6sys.stdout.write("C\n")

print produced spaces automatically. sys.stdout.write did not. That extra control can matter for progress bars, terminal effects, or protocols that need precise output.

Use print(..., end="") when you want convenience without newlines

Many cases that look like sys.stdout.write use cases can still be handled cleanly with print by adjusting end.

python
for i in range(3):
    print(i, end=" ")
print()

This keeps the friendly formatting behavior while avoiding the default newline after every call.

sys.stdout.write is useful for precise terminal control

If you are updating one line repeatedly, writing directly to stdout is often clearer.

python
1import sys
2import time
3
4for i in range(5):
5    sys.stdout.write(f"\rprogress: {i + 1}/5")
6    sys.stdout.flush()
7    time.sleep(0.2)
8
9sys.stdout.write("\n")

The explicit flush matters because some environments buffer output.

One reason print remains useful is that it can write to any file-like object through the file argument while keeping its formatting behavior.

python
with open("output.txt", "w", encoding="utf-8") as handle:
    print("first", "second", sep=" | ", file=handle)

sys.stdout.write can do the same job only if you already have the stream object and want to call its write method directly. So the difference is not that one can redirect and the other cannot. The difference is that print wraps redirection with friendly formatting, while sys.stdout.write stays closer to the underlying stream API.

That makes print a better default for application code and sys.stdout.write a better fit for stream-oriented output logic.

Performance differences are usually secondary

People sometimes ask which one is faster. In normal application code, readability matters much more than the tiny overhead difference. If output performance is a real bottleneck, measure it in the actual environment. For most scripts and services, the ergonomic advantages of print dominate.

Common Pitfalls

  • Forgetting that sys.stdout.write does not append a newline automatically.
  • Passing non-string values to sys.stdout.write and getting a type error.
  • Replacing print with low-level writes in ordinary code without any real benefit.
  • Forgetting to flush stdout when implementing progress indicators.
  • Assuming print cannot control separators or line endings even though sep and end exist.

Summary

  • Use print for most human-readable console output.
  • Use sys.stdout.write when you need exact control over written text.
  • 'print adds separators and a newline by default.'
  • 'sys.stdout.write accepts only strings and returns the number of characters written.'
  • Reach for low-level stdout writes mainly in precise formatting or streaming scenarios.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.