📚
Print to stderr
Where Does Output Go?
When you use
print()- ◆Standard Output (stdout) — for normal messages. This is where sends text by default.
print() - ◆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
sysimport sys
Then use the
fileprint()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
. But knowing about stderr helps you understand how real software handles errors.print()
main.pySandbox
main.py
Ctrl + Enter