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 prints debug message using sys.stderr. Stderr always used for printing errors and it is beneficial if you want to separate warning and error messages 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:
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, 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:
- How to Create an Empty Tuple in Python
- Python create empty set
- Python check if a variable is an integer
- Python program to print prime numbers
In this Python Tutorial, we learned – Python print to stderr, Python takes input from stdin, Python stdout, and Read from stdin in Python with examples.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.