InterviewSolution
| 1. |
Are there any alternatives to standard input/output functions of Python? |
|
Answer» Python’s built-in FUNCTION library provides input() and print() functions. They are default functions for console IO. The input() function receives data received from standard input stream i.e. keyboard and print() function sends data to standard output stream directed to standard output device i.e. DISPLAY monitor device. In addition to these, we can make use of standard input and output stream objects defined in sys module. The sys.stdin OBJECT is a file like object capable of reading data from a console device. It in fact is an object of TextIOWrapper class capable of reading data. Just as built-in File object, read() and readline() methods are defined in TextIOWrapper. >>> import sys >>> a=sys.stdin >>> type(a) <class '_io.TextIOWrapper'> >>> data=a.readline() Hello Python >>> data 'Hello Python\n'Note that unlike input() function, the TRAILING newline character (\n) is not stripped here. On the other hand sys.stdout object is also an object of TextIOWrapper class that is configured to write data in standard output stream and has write() and writelines() methods. >>> b.write(data) Hello Python 13 |
|