Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between raw_input() and input()

Detailed explanation of the difference between raw_input() and input()

高洛峰
高洛峰Original
2017-03-16 17:13:581488browse

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 "", line 1, in 
  File "", line 1, in 
NameError: name 'PythonTab' is not defined
>>> 
>>> input_A = input("Input: ")
Input: "PythonTab.com"
>>> print input_A
PythonTab.com
>>>

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)

>>> input_B = input("input: ")
input: 2015
>>> type(input_B)

>>>

Example 1 You can see: Both functions can receive String, but raw_input() Read input directly from the console (it can accept any type of input). As for input(), it hopes to be able to read a legal python expression, that is, when you enter a character string, you must use quotation marks to surround it, otherwise it will trigger 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 number entered (int, float); at the same time, as shown in Example 1, input() can accept legal python expressions. Formula, for example: input(1 + 3) will return 4 of type int.

Check the python manual and learn:

input([prompt])

Equivalent to eval(raw_input(prompt))

input() is essentially implemented using raw_input(). It just calls the eval() function after calling raw_input(). Therefore, you can even use an expression as a parameter of input(), and it will calculate the expression. The value of the formula and returns 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 something special about input() Required, otherwise we generally recommend using raw_input() to interact with users.

The above is the detailed content of Detailed explanation of the difference between raw_input() and input(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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