Difference between input () and raw_input ()

11

I am informing myself with the functions to interact with the user. I'm with the basics: raw_input() e input() . I have read that input() only takes the intenger data, which does not accept strings, and that we use raw_input() for that.

The problem is that if I store a string in a variable with input() if it accepts it and recognizes it. But if I try to do the same with raw_input() I get the following error:

Traceback (most recent call last):
  File "lllll.py", line 9, in <module>
    raw_input()
NameError: name 'raw_input' is not defined

Working with Python 3.5.2

    
asked by Jogofus 09.12.2016 в 02:38
source

3 answers

17

The error occurs, as you have been told, because raw_input does not exist in Python 3.X . This has been and will continue to be a source of confusion because the input() functions do not do the same in Python 2 as in Python 3 despite being called equal.

In Python 2.x there are both functions, let's clarify a little that each one does:

Python 2.x :

  • raw_input() returns a line from the post's entry user as is, raw and always returns a character string , an object str (ASCII) containing what was entered.

    >>> v = raw_input('Introduce algo: ')
    Introduce algo: 24
    >>> print v
    24
    

    But we must remember that the variable v it does not contain a number, it is not a int , it is a string of characters (text). If we try to operate as an integer:

    >>> v + 6
    

    We jump an error telling us that we can not add a string with an integer:

    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        v + 6
    TypeError: cannot concatenate 'str' and 'int' objects
    

    To be an integer, you have to convert it by doing an explicit casting (Python has a strong typing, it never makes an implicit casting to adapt the type of the variable to the context):

    >>> v = int(v)
    >>> v + 6
    30
    
  • input() expected to be pass valid Python expressions . That is, you can only pass code Python and the function evaluates and processes as such.

    >>> v = input('Ingresa algo que pueda evaluar: ')
    Ingresa algo que pueda evaluar: 2 + 5 * 3
    >>> print v
    17
    

    What has happened here? Well, input() has taken the expression and since it is valid Python code, it has evaluated it and performed the operations. We could think that input() only accepts numbers but that is false, accepts any valid expression in Python:

    Concatenating two chains:

    >>> v = input('Ingresa algo que pueda evaluar: ')
    Ingresa algo que pueda evaluar: 'hola ' + 'mundo'
    >>> print(v)
    'hola mundo'
    

    Creating a list using list compression:

    >>> v = input('Ingresa algo que pueda evaluar: ')
    Ingresa algo que pueda evaluar: [n *2 for n in range (10)]
    >>> print(v)
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    

    We can pass a string without problems, but a string is passed to it (using the quotes):

    >>> v = input('Ingresa algo que pueda evaluar: ')
    Ingresa algo que pueda evaluar: "hola mundo"
    >>> print(v)
    hola mundo
    

Python 3.x:

  • input() does the same thing as raw_input() in Python 2.x . Reads a line from the input of the standard input and returns it raw in an object str (UTF-8). In fact, it is the same function but renamed, the only difference is that in Python 2 str is ASCII while in Python 3 it is a Unicode string (UTF-8).

  • The old input() equals eval() , which takes a string of text and evaluate it as if it were normal Python code:

    >>> v = eval(input('Ingresa algo que pueda evaluar: '))
    Ingresa algo que pueda evaluar: 'hola ' + 'mundo'
    >>> print(v)
    'hola mundo'
    

In short, if you use Python 3.x there is only input() and a string always returns. If you use Python 2.x raw_input do the same as input() in Python 3.x (accept any string and always return an object str ) while input() only accept expressions that are syntactically valid in Python (otherwise will produce an error) and evaluates them later.

I advise you that when you look at documentation or tutorials you can see which version they use because the changes from branch 2 to 3 were important. Here is a list of the most significant (in English):

link

  

Warning: The use of input (Python 2.x) or eval should be very careful and never use them to fetch unfiltered user entries. Remember that they accept Python code and evaluate it. A user can enter harmful code for the program or system, both involuntarily and maliciously. An example, if we have the module os imported, nothing prevents a user if the script has sufficient privileges to enter os.system('rm -rf /') and send all the files of the system to a better life. exec , eval e input (Python 2) expose to injection of malicious code , this does not mean that they are the devil and that they should not exist, they are very useful and powerful in many cases , but we must be aware and consistent of its power and danger .

    
answered by 09.12.2016 / 04:59
source
6

The raw_input() function was renamed to input() in Python 3.x

The old input() can be emulated with eval(input())

    
answered by 09.12.2016 в 03:12
3

For python2 there are 2 functions input() and raw_input() , the first is for numeric values, the second one is for text strings; but this changes in python3 , the raw_input() has been renamed to input (), and the old input() has been removed. So if you use python 3 you should use input() instead of raw_input()

    
answered by 09.12.2016 в 03:10