What's the difference between `raw_input()` and `input()` in Python 3?

后端 未结 6 1682
执笔经年
执笔经年 2020-11-21 04:19

What is the difference between raw_input() and input() in Python 3?

相关标签:
6条回答
  • 2020-11-21 04:45

    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.

    0 讨论(0)
  • 2020-11-21 04:46

    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()).

    0 讨论(0)
  • 2020-11-21 04:50

    I'd like to add a little more detail to the explanation provided by everyone for the python 2 users. raw_input(), which, by now, you know that evaluates what ever data the user enters as a string. This means that python doesn't try to even understand the entered data again. All it will consider is that the entered data will be string, whether or not it is an actual string or int or anything.

    While input() on the other hand tries to understand the data entered by the user. So the input like helloworld would even show the error as 'helloworld is undefined'.

    In conclusion, for python 2, to enter a string too you need to enter it like 'helloworld' which is the common structure used in python to use strings.

    0 讨论(0)
  • 2020-11-21 04:51

    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. Try to use safer ways of parsing your input if possible.)

    0 讨论(0)
  • 2020-11-21 05:00

    In Python 3, raw_input() doesn't exist which was already mentioned by Sven.

    In Python 2, the input() function evaluates your input.

    Example:

    name = input("what is your name ?")
    what is your name ?harsha
    
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        name = input("what is your name ?")
      File "<string>", line 1, in <module>
    NameError: name 'harsha' is not defined
    

    In the example above, Python 2.x is trying to evaluate harsha as a variable rather than a string. To avoid that, we can use double quotes around our input like "harsha":

    >>> name = input("what is your name?")
    what is your name?"harsha"
    >>> print(name)
    harsha
    

    raw_input()

    The raw_input()` function doesn't evaluate, it will just read whatever you enter.

    Example:

    name = raw_input("what is your name ?")
    what is your name ?harsha
    >>> name
    'harsha'
    

    Example:

     name = eval(raw_input("what is your name?"))
    what is your name?harsha
    
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        name = eval(raw_input("what is your name?"))
      File "<string>", line 1, in <module>
    NameError: name 'harsha' is not defined
    

    In example above, I was just trying to evaluate the user input with the eval function.

    0 讨论(0)
  • 2020-11-21 05:07

    If You want to ensure, that your code is running with python2 and python3, use function input () in your script and add this to begin of your script:

    from sys import version_info
    if version_info.major == 3:
        pass
    elif version_info.major == 2:
        try:
            input = raw_input
        except NameError:
            pass
    else:
        print ("Unknown python version - input function not safe")
    
    0 讨论(0)
提交回复
热议问题