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.
Regarding raw_input.split() function in python
First, input()
prompts the user for input. The user enters a series of characters that is a string. Then, the split()
function separates the string from the user into a list of words (or groups of characters) and returns list of these words or grouped characters. By default, each element in the list is separated by a blankspace. See the below examples.
words = input().split() # Typed "These are the input words"
print(words)
Output:
['These', 'are', 'the', 'input', 'words']
You can also specify a specific character other than a blank space to split on in the split()
method. For example, in this instance, the input string is split upon occurrences of a '1'
.
words = input().split('1') # Typed "100120023104"
print(words)
Output:
['', '00', '20023', '04']
Note that the split()
will omit the character requested to split the input string on (blankspace ' ' by default) in the returned list.
Also, raw_input()
is no longer supported in Python3. raw_input()
was supported by older versions of Python2. Now, we just use input()
in Python3 and it works the same way. You should upgrade your environment to Python3 if you are still using raw_input()
.
(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:
When use Python3.8:
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.
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.)
Understanding raw_input function in Python
When you type a response to the raw_input() you finish by entering a newline. Since each character you enter is also displayed as you type, the newline shows up as well.
If you modified the python builtins and changed raw_input you could get it to terminate on '.' instead of '\n'. An interaction might look like:How old are you? 12.How tall are you? 12.How much do you weigh? 12. So you're ...
How do I insert a variable into a raw_input query?
try:
age = raw_input("How old are you, %s? " % name)
Explanation:
raw_input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
So, when you do
age = raw_input("How old are you, %s? ") % name
let's say you entered Paul
so the above statement becomes,
age = "Paul" % name
and since string "Paul" is not a placeholder it throws the corresponding error.
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?
Is it ever useful to use Python's input over raw_input?
Is it ever useful to use Python 2's input over raw_input?
No.
input()
evaluates the code the user gives it. It puts the full power of Python in the hands of the user. With generator expressions/list comprehensions, __import__
, and the if/else
operators, literally anything Python can do can be achieved with a single expression. Malicious users can use input()
to remove files (__import__('os').remove('precious_file')
), monkeypatch the rest of the program (setattr(__import__('__main__'), 'function', lambda:42)
), ... anything.
A normal user won't need to use all the advanced functionality. If you don't need expressions, use ast.literal_eval(raw_input())
– the literal_eval
function is safe.
If you're writing for advanced users, give them a better way to input code. Plugins, user modules, etc. – something with the full Python syntax, not just the functionality.
If you're absolutely sure you know what you're doing, say eval(raw_input())
. The eval
screams "I'm dangerous!" to the trained eye. But, odds are you won't ever need this.
input()
was one of the old design mistakes that Python 3 is solving.
Related Topics
Why Does Indexing Numpy Arrays with Brackets and Commas Differ in Behavior
Do I Need to Import Submodules Directly
String Comparison Doesn't Seem to Work for Lines Read from a File
How to Get Dict from SQLite Query
What Is Sys.Maxint in Python 3
Getting Values from JSON Using Python
How to Control the Mouse in MAC Using Python
Removing Entries from a Dictionary Based on Values
Recursive Definitions in Pandas
How to Find Unused Functions in Python Code
Rect Collision with List of Rects
How to Make a Python Script Executable
Why Should I Close Files in Python