A little difference between input and raw_input in Python

  • 2020-04-02 14:17:54
  • OfStack

Both input and raw_input can be used to read console input, but there are differences between input and raw_input when dealing with Numbers

When the input is a pure number:

Input returns numeric types such as int and float
Raw_inpout returns a string type, a string type

The input string is an expression

Input evaluates a numeric expression in a string, while raw_input does not.

For example, enter "57 + 3" :

The input will give you the integer 60

Raw_input gets the string "57 + 3"

Implementation of python input

Looking at the python input document, you can see that input is actually implemented by raw_input. The principle is very simple, just the following line of code:


def input(prompt):
    return (eval(raw_input(prompt)))


Related articles: