Print a line to STDERR and STDOUT in Python
In this tutorial, we will discuss how to print a line to STDERR and STDOUT in Python.
In Python, there are standard terms like Stdin, stderr, stdout, etc.
Let’s see about the terms,
Standard Input (stdin)
Standard Input reads the data from the user Input, and it acts based on the user instructions. It returns the user Input data as per the instruction given in the Program.
For this, Standard terms we need to import the sys library module.
import sys Python = sys.stdin for Lan in Python: if 'Stop' == Lan.strip(): print('Found Stop so Terminating') exit(0) else: print('Message from User: {}'.format(line))
Output:
hello world Message from user: hello world Stop Found Stop so Terminating
Standard Output (stdout)
Standard Output is used to return the User input of words directly to the Console.
import sys Python = sys.stdout String = 'Hello world', 'Welcome to Python' for Lan in String: Python.write(Lan)
Output:
Hello world welcome to Python
Standard Error (stderr)
Standard Error is used to return the data directly to the Console and it also return the error messages.
Here, we are trying to add integer to the string.
import sys Hello = sys.stdout World = sys.stderr Lan = 'Welcome', 'to Python' for i in Lan: Hello.write(i) try: i += 2 except: World.write('Error')
Output:
Welcometo python errorerror
Also read: howdoi in Python
Leave a Reply