Python – stderr, stdin and stdout

By: On:

In this python tutorial, you will learn about python print – stderr, stdin, and stdout with examples.

Python provides us with file-like objects that represent stdin, stdout, and stderr. So first we need to import the sys module in python. Here we will see how we can work with these objects.

Contents


Python print to stderr

Python stderr is known as a standard error stream. It is similar to stdout because it also directly prints to the console but the main difference is that it only prints error messages.

Example:

import sys
sys.stderr.write("This is error msg")

After writing the above code (python print to stderr), you can observe that it print debug message using sys.stderr. Stderr always used for printing errors and it is beneficial if you want to separate warning and error message from the actual output.

You can refer to the below screenshot for python print to stderr.


Python print to stderr
Python print to stderr

Python take input from stdin

Python stdin is used for standard input as it internally calls the input function and the input string is appended with a newline character in the end. So, use rstrip() function to remove it.

Example:

import sys
for line in sys.stdin:
if 'E' == line.rstrip():
break
print(f"Message : {line}')
print("End")

After writing the above code (python take input from stdin), the program reads the user message from standard input and processes it accordingly. The program will terminate when the user will input the “E” message and it prints “End”.

You can refer to the below screenshot for python take input from stdin.

Python take input from stdin
Python take input from stdin

Python stdout

Python stdout is known as standard output. Here, the write function will print directly whatever string you will give.

Example:

import sys
s = sys.stdout
my_input = ['Welcome', 'to', 'python']
for a in my_input:
s.write(a + '\n')

After writing the above code (python stdout), the output will be ” Welcome to python”. We get the output to the console as we write sys.stdout. So, whatever input is given we will see on the console.

You can refer to the below screenshot for python stdout.

Python stdout
Python stdout

Read from stdin in python

To read an input from stdin we can call read() and readlines() function in Python, for reading everything.

Example:

from sys import stdin
my_input = stdin.read(1)
my_input2 = stdin.readline()
total = int(my_input2)
print("my_input = {}".format(my_input))
print("my_input2 = {}".format(my_input2))
print("total = {}".format(total))

After writing the above code (read from stdin in python), the stdin will read input and prints the input for each.

You can refer to the below screenshot for read from stdin in python.

Read from stdin in python
Read from stdin in python

You may like the following Python tutorials:

In this Python Tutorial, we learned – Python print to stderr, Python takes input from stdin, Python stdout, and Read from stdin in python with example.

Leave a Comment