Both Raw_input and input two are Python built-in functions that interact with user implementations by reading the console's input. But they have different functions. Here are two examples to illustrate the difference in use.
Example 1
Python 2.7.5 (default, Nov, 16:26:36) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on Linux2type "help", "copyright", "C Redits "or" license "for more information.>>> >>> raw_input_a = Raw_input (" raw_input: ") Raw_input:pyth ontab.com>>> print raw_input_a pythontab.com>>> input_a = input ("Input:") Input: Pythontab.comtraceback (most recent): File "
", line 1, in
file "
", line 1, in
nameerror:name ' pythontab ' are not defined>>> >>> input_a = input (" Input: ") Input:" pythontab.com ">>> print input_apythontab.com>>>
Example 2
Python 2.7.5 (default, Nov, 16:26:36) [GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on Linux2type "help", "copyright", "C Redits "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 can be seen: both functions can receive a string, but raw_input () directly read the console input (any type of input it can be received). For input (), it wants to be able to read a valid Python expression, that is, you must enclose it in quotation marks when you enter a string, or it will throw a syntaxerror.
Example 2 can be seen: Raw_input () treats all inputs as strings, returning string types. Input () has its own characteristics when it treats a pure digital input, it returns the type of the number entered (int, float), and in Example 1, input () accepts a valid Python expression, for example: input (1 + 3) returns the 4 of the int type. 。
Check out the Python manual to learn:
Input ([prompt])
Equivalent to eval (raw_input (Prompt))
Input () is actually implemented using Raw_input (), just after calling Raw_input () and then invoking the Eval () function, so you can even use the expression as an argument to input (), and it evaluates the expression and returns it.
In built-in Functions, however, there is a sentence that reads: Consider using the Raw_input () function for general input from users.
Unless there is a special need for input (), it is generally recommended to use raw_input () to interact with the user.