Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between input() and sys.stdin?

Tags:

python

input

sys

I am new to python and trying some coding problems online. i come across sys.sdnin a lot for accepting input. I would like to know how input() and sys.stdin differs in action?

like image 339
raj Avatar asked Apr 08 '15 23:04

raj


People also ask

What does Sys stdin mean?

Using sys. Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it.

Is SYS stdin readline faster than input?

stdin. readline() is the fastest one when reading strings and input() when reading integers.

Is stdin faster than input Python?

stdin. readline is actually for Faster Inputs, because line reading through System STDIN (Standard Input) is faster in Python.


1 Answers

Clarification through Code Examples

I've been asking myself the same question, so I came up with these two snippets, which clarify how sys.stdin and input() differ by emulating the latter with the former:

import sys

def my_input(prompt=''):
  print(prompt, end='') # prompt with no newline
  for line in sys.stdin:
    if '\n' in line: # We want to read only the first line and stop there
      break
  return line.rstrip('\n')

Here is a more condensed version:

import sys

def my_input(prompt=''):
  print(prompt, end='')
  return sys.stdin.readline().rstrip('\n')

Both these snippets differ from the input() function in that they do not detect the End Of File (see below).

Clarification through Documentation

This is how the official documentation describes the function input():

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

And here's how sys.stdin is described:

sys.stdin

File object used by the interpreter for standard input.
stdin is used for all interactive input (including calls to input());
These streams (sys.stdin, sys.stdout and sys.stderr) are regular text files like those returned by the open() function. [...]

So whereas input() is a function, sys.stdin is an object (a File object). As such, it has a number of attributes, which you can explore in the interpreter, with:

> dir(sys.stdin)

['_CHUNK_SIZE',
 '__class__',
 '__del__',
 '__delattr__',
 '__dict__',
 '__dir__',

  ...

 'truncate',
 'writable',
 'write',
 'write_through',
 'writelines']

and which you can display individually, for instance:

> sys.stdin.mode
r

It also has methods, such as readline(), which "reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline." (1)

Full implementation

This last method allows us to fully emulate the input() function, including its EOF Exception error:

def my_input(prompt=''):
  print(prompt, end='')
  line = sys.stdin.readline()
  if line == '': # readline() returns an empty string only if EOF has been reached
    raise EOFError('EOF when reading a line')
  else:
    return line.rstrip('\n')
like image 102
kotchwane Avatar answered Nov 04 '22 22:11

kotchwane