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.
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 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 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.
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.
You may like the following Python tutorials:
- Increment and Decrement operators in Python
- Object oriented programming python
- Constructor in Python
- Python access modifiers + Examples
- Python Anonymous Function
- Python Read CSV File and Write CSV File
- Python Array with Examples
- Python get filename from the path
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.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.