input() and input() in python3.x?

python3.x中raw_input()input()之间有什么区别?


The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input() , and the old input() is gone, but can easily be simulated by using eval(input()) . (Remember that eval() is evil, so if try to use safer ways of parsing your input if possible.)


In Python 2 , raw_input() returns a string, and input() tries to run the input as a Python expression.

Since getting a string was almost always what you wanted, Python 3 does that with input() . As Sven says, if you ever want the old behaviour, eval(input()) works.


Python 2:

  • raw_input() takes exactly what the user typed and passes it back as a string.

  • input() first takes the raw_input() and then performs an eval() on it as well.

  • The main difference is that input() expects a syntactically correct python statement where raw_input() does not.

    Python 3:

  • raw_input() was renamed to input() so now input() returns the exact string.
  • Old input() was removed.
  • If you want to use the old input() , meaning you need to evaluate a user input as a python statement, you have to do it manually by using eval(input()) .

    链接地址: http://www.djcxy.com/p/9652.html

    上一篇: 在cmd.Cmd中完成时,sys.stdout已被替换

    下一篇: 输入()和输入()在python3.x?