input() and raw_input()

Asked

Viewed 1,216 times

1

According to the Python 2.x documentation, it is recommended to use the function raw_input instead of input. Conversely, in the Python 3.x documentation, the function raw_input doesn’t even appear. Then the function input in Python 3.x replaces function raw_input?

2 answers

3

Yes, the input of Python 3.x is the raw_input python 2.x. This change can be found in changelog of version 3.0:

PEP 3111: raw_input() was renamed to input(). That is, the new input() Function reads a line from sys.stdin and Returns it with the trailing newline Stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).

For more details, see PEP 3111.

1


In Python 2 there are functions input and raw_input, but the first is rarely used because it only accepts syntactically valid literals, i.e., numbers, or strings between quotation marks, etc. The function raw_input is much more useful: it accepts any string, and it’s your responsibility to deal with it.

In Python 3 the function raw_input came to be called input, and the old function input virtually useless was removed.

In Python 3 there were several "cleanings" of this type.

  • 3

    I wouldn’t say they took out the input because it’s a useless function. The bigger problem is that the eval of input in Python 2.x allows the user to execute arbitrary Python code. This can be very dangerous.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.