What's the Difference Between 'Raw_Input()' and 'Input()' in Python 3

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

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

Differences between `input` and `raw_input`

In python 2.x, raw_input() returns a string and input() evaluates the input in the execution context in which it is called

>>> x = input()
"hello"
>>> y = input()
x + " world"
>>> y
'hello world'

In python 3.x, input has been scrapped and the function previously known as raw_input is now input. So you have to manually call compile and than eval if you want the old functionality.

python2.x                    python3.x

raw_input() --------------> input()
input() -------------------> eval(input())

In 3.x, the above session goes like this

>>> x = eval(input())
'hello'
>>> y = eval(input())
x + ' world'
>>> y
'hello world'
>>>

So you were probably getting an error at the interpretor because you weren't putting quotes around your input. This is necessary because it's evaluated. Where you getting a name error?

(python 3) When using raw_input(), why does the function add \n to the input?

In Python3.x, raw_input() and input() are integrated, raw_input() is removed, only the input() function is retained, which receives any input, defaults all input to string processing, and returns the string type.

When use Python2.7:

Sample Image

When use Python3.8:

Sample Image

In addition, if you use extensions for debugging other than the python extension in VS Code, please try to disable it to avoid affecting the results.

My environment:

VS Code: 1.52.1 (user setup);
OS: Windows_NT x64;
VS Code extension: Python; Pylance;

Reference: What's new in Python3.

raw_input function in Python

It presents a prompt to the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string. See the docs for raw_input().

Example:

name = raw_input("What is your name? ")
print "Hello, %s." % name

This differs from input() in that the latter tries to interpret the input given by the user; it is usually best to avoid input() and to stick with raw_input() and custom parsing/conversion code.

Note: This is for Python 2.x

How do I use raw_input in Python 3?

Starting with Python 3, raw_input() was renamed to input().

From What’s New In Python 3.0, Builtins section second item.

conditional statement using dictionary isn't working from python3 to python2.4

Python3 replaced the old input statement with the functionality of python 2’s raw_input. It used to evaluate the input now it’s passed as a string for safety.

Replace the line: (py3)

answer = input('which test? ')

With: (py2)

answer = raw_input('which test? ')

Or:

answer = str(input('which test? '))

Refer to PEP3111 for more details.



Related Topics



Leave a reply



Submit