Python Print To stdout

3 Best Examples for Python Print To stdout

In this article, I’ll shed light on Python print to stdout and stderr.

Python’s sys module provides all three file objects for stdin, stdout, and stderr. For the input file object, we use sys.stdin. This is similar to a file, that you can open and close from any other file.

In Python, stdout stands for standard output and is used to output text of any command you type in the terminal, and then that output is stored in the stdout stream.

On the contrary, stderr is the standard error message that is used to print the output on the console screen or windows terminal. Stderr is also one of the command outputs as stdout, which is logged anywhere by default.

Let me put it this way, whenever you use the print() function in Python, it refers to the sys.stdout, the input() function comes from sys.stdin, and the exceptions are written in sys.stderr.

Python print to stdout

By using the command print() we are using stdout in Python. Stdout stands for standard output. It stores the output in the stdout stream.

Python print to stdout Explained

First and foremost, it is important to understand how sys.stdout works in Python.

The sys.stdout function is a system library function that works exactly like a sys.stdin function for displaying the output on the console or terminal screen.

Whereas the sys.stdin function is similar to a file, where you can open and shut it, just like any other file. Let me give you an example of sys.stdin and sys.stdout. 

sys.stdin in Python Example

import sys
stdin = sys.stdin
for string in stdin:
if 'Daniel' == string.strip():
    print("Found Daniel. Terminating the program")
    exit(0)
else:
    print("Message from sys.stdin: ---> {} <---".format(string))

Output:

Hello
Message from sys.stdin: —> Hello <—

Daniel
Found Daniel. Terminating the program.

sys.stdout in Python Example

import sys
stdout = sys.stdout
customInput = ["Hello", "Daniel", "Exit"]
for my_input in customInput:
stdout.write(my_input + "\n").

Output:

Hello
Daniel
Exit

Does it look good now? Well, there are actually three different ways to print to the stdout stream in Python. 

3 Best Ways to print to the stdout stream in Python

1.Using the Python print() Function Example

I’m assuming that you are familiar with the print() function. Hence, you are already into Python programming.

The print() function is the most straightforward function in Python. It doesn’t require any parameters but an empty parenthesis.

It commands the interpreter to execute the code rather than calling it by name.

print("This is a Standard Output!")

Output:

This is a Standard Output!

But if you’re expecting to write Standard Error (stderr), you must import the sys library into your program.

#Imports System Library
import sys
print("This is an Error Message!", file=sys.stderr)

2. Using the Python sys.stdout.write() Function Examle

The write() function does the same thing as the print() function. The only exception is that it doesn’t print the numbers of letters within the text when used in interactive mode.

import sys
bar = 5
sys.stdout.write("This is a Standard Output!\n")
sys.stderr.write("This is an Error Message!\n" )
sys.stdout.write(bar)

Output:

This is a Standard Output!
This is an Error Message!

Traceback (most recent call last):
  File “script.py”, line 5, in <module>
sys.stdout.write(bar)
TypeError: write() argument must be str, not int.