The difference between raw_input() and input()

高洛峰
Release: 2016-10-17 13:19:03
Original
1136 people have browsed it

raw_input and input are both built-in functions of Python, which interact with the user by reading input from the console. But their functions are not the same. Here are two examples to illustrate the difference in usage.

Example 1

Python 2.7.5 (default, Nov 18 2015, 16:26:36) 
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> raw_input_A = raw_input("raw_input: ")
raw_input: PythonTab.com
>>> print raw_input_A 
PythonTab.com
>>> input_A = input("Input: ")
Input: PythonTab.com
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name &#39;PythonTab&#39; is not defined
>>> 
>>> input_A = input("Input: ")
Input: "PythonTab.com"
>>> print input_A
PythonTab.com
>>>
Copy after login

Example 2

Python 2.7.5 (default, Nov 18 2015, 16:26:36) 
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> raw_input_B = raw_input("raw_input: ")
raw_input: 2015
>>> type(raw_input_B)
<type &#39;str&#39;>
>>> input_B = input("input: ")
input: 2015
>>> type(input_B)
<type &#39;int&#39;>
>>>
Copy after login

Example 1 As you can see: both functions can receive strings, but raw_input() directly reads the input from the console (it can receive any type of input). As for input(), it hopes to be able to read a legal python expression, that is, you must use quotes to enclose it when you enter a string, otherwise it will raise a SyntaxError.

Example 2 You can see: raw_input() treats all inputs as strings and returns the string type. And input() has its own characteristics when dealing with pure numeric input. It returns the type of the input number (int, float); at the same time, as shown in Example 1, input() can accept legal python expressions, for example: input( 1 + 3) will return 4 as an int.

Check the python manual and learn that:

input([prompt])

Equivalent to eval(raw_input(prompt))

input() is essentially implemented using raw_input(), just after calling raw_input() The eval() function is then called, so you can even pass an expression as an argument to input() and it will evaluate the expression and return it.

But there is a sentence in Built-in Functions that says: Consider using the raw_input() function for general input from users.

Unless there is a special need for input(), we generally recommend it. raw_input() to interact with the user.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template