Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a line from standard input in Python

What (if any) are the differences between the following two methods of reading a line from standard input: raw_input() and sys.stdin.readline() ? And in which cases one of these methods is preferable over the other ?

like image 607
Grigor Gevorgyan Avatar asked Aug 06 '11 10:08

Grigor Gevorgyan


People also ask

How do you input a line in Python?

The input() function reads a line from the input (usually from the user), converts the line into a string by removing the trailing newline, and returns it. If EOF is read, it raises an EOFError exception.

Which built in Python function reads from standard input?

The easiest way to obtain user input from the command-line is with the raw_input() built-in function. It reads from standard input and assigns the string value to the variable you designate. You can use the int() built-in function (Python versions older than 1.5 will have to use the string.

What is standard input in Python?

Standard input – This is the file-handle that a user program reads to get information from the user. We give input to the standard input (stdin). Standard output – The user program writes normal information to this file-handle. The output is returned via the Standard output (stdout).

What is SYS stdin read ()?

The sys. stdin is another way is to read from the standard input the calls input() function internally. Python has another module named fileinput for reading the standard input. The input() function of this module can be used to read standard input or read content from one or more files.


2 Answers

raw_input() takes an optional prompt argument. It also strips the trailing newline character from the string it returns, and supports history features if the readline module is loaded.

readline() takes an optional size argument, does not strip the trailing newline character and does not support history whatsoever.

Since they don't do the same thing, they're not really interchangeable. I personally prefer using raw_input() to fetch user input, and readline() to read lines out of a file.

like image 69
Frédéric Hamidi Avatar answered Oct 05 '22 05:10

Frédéric Hamidi


"However, from the point of view of many Python beginners and educators, the use of sys.stdin.readline() presents the following problems:

  1. Compared to the name "raw_input", the name "sys.stdin.readline()" is clunky and inelegant.

  2. The names "sys" and "stdin" have no meaning for most beginners, who are mainly interested in what the function does, and not where in the package structure it is located. The lack of meaning also makes it difficult to remember: is it "sys.stdin.readline()", or " stdin.sys.readline()"? To a programming novice, there is not any obvious reason to prefer one over the other. In contrast, functions simple and direct names like print, input, and raw_input, and open are easier to remember." from here: http://www.python.org/dev/peps/pep-3111/

like image 32
Mariy Avatar answered Oct 05 '22 07:10

Mariy