📚

Print to stderr

Where Does Output Go?

When you use

print()
, the text appears on your screen. But behind the scenes, your program actually has two separate output channels — think of them as two different pipes:

  • Standard Output (stdout) — for normal messages. This is where
    print()
    sends text by default.
  • Standard Error (stderr) — a separate channel specifically for error messages.

Why does this matter? Imagine you have a program that processes data and occasionally encounters errors. If both normal output and errors go to the same place, they get mixed together. By sending errors to a separate channel, you (or other programs) can handle them differently.

How to Print to stderr

First, you need to import the

sys
module (a built-in library that gives you access to system features):

import sys

Then use the

file
parameter in
print()
to redirect where the output goes:

import sys
print("This is a normal message")                  # Goes to stdout
print("This is an error!", file=sys.stderr)         # Goes to stderr

Both messages appear on your screen, but they traveled through different channels.

💡

💡 Don't worry if this feels advanced — in most programs you'll just use regular

print()
. But knowing about stderr helps you understand how real software handles errors.

main.pySandbox
main.py