Functions: Raw_input () and input ()
Note: In python3.x, the Raw_input () has been removed, instead of input (), which is only renamed, and the usage is the same. So here is the introduction of raw_input () and input () in python2.x, as long as the Raw_input () is used in python3.x.
1: Function: Read the console input and user implementation interaction
2: Grammar
Raw_input ([prompt])
Input ([prompt])
3: Parameters
Prompt: If this parameter is present, it will be output directly to the screen and will not go down another line
4: The relationship between the two:
Input () is essentially implemented using raw_input (), which calls the Eval () function after Raw_input () is called, and is called as follows:
def input (prompt):
Return (eval (raw_input (prompt)))
5: The two points are the same:
can accept strings, numbers, and expressions as input.
6: The difference between the two:
6.1. When input is a string:
Raw_input (): reads the console input while returning the string type
Input (): reads the input from the console, but must be enclosed in quotation marks when entering, otherwise it will be an error.
6.2. When the input is a pure number:
Raw_input (): Reads the console input and returns the string type as String processing
Input (): Reads the console input, returns the input numeric type (int, float)
6.3. When the input is a string expression:
Raw_input (): reads the input from the console, but does not operate on the input numbers, directly returning the string type as String processing
Input (): reads the input from the console, evaluates a valid Python numeric expression, and returns the result of the operation
6.4, the input for special characters
Like ' \ t ', ' \ n ', etc.
Raw_input (): reads input from console, returns string type, same as input
Input (): reads the input from the console, but must be enclosed in quotation marks to return the content represented by the special symbol
Note: It is recommended to use Raw_input () to interact with users without special requirements
7: Example:
7.1. When entering as a string:
>>> A1 = Raw_input ("RAW_INPUT_STR:")
Raw_input_str:hello
>>> Print A1,type (A1)
Hello <type ' str ' >
>>> A2 = input ("INPUT_STR:")
Input_str:hello
Traceback (most recent):
File "<pyshell#4>", line 1, in <module>
A2 = input ("Input:")
File "<string>", line 1, in <module>
Nameerror:name ' Hello ' is not defined
>>> A2 = input ("INPUT_STR:")
Input_str: ' Hello '
>>> Print A2,type (A2)
Hello <type ' str ' >
7.3. When entering as a string expression:
>>> C1 = Raw_input ("Raw_input_exp:")
Raw_input_exp:3 + 3
>>> print C1,type (C1)
3 + 3 <type ' str ' >
>>> C2 = input ("Input_exp:")
Input_exp:3 + 3
>>> print C2,type (C2)
6 <type ' int ' >
7.4, the input for special characters when:
>>> D1 = raw_input ("raw_input_sp:")
RAW_INPUT_SP: \ t
>>> print D1,type (d1)
\ t <type ' str ' >
>>> D2 = input ("INPUT_SP:")
INPUT_SP: \ t
Traceback (most recent):
File "<pyshell#57>", line 1, in <module>
D2 = input ("INPUT_SP:")
File "<string>", line 1
\ t
^
syntaxerror:unexpected character after line continuation character
>>> D2 = input ("INPUT_SP:")
INPUT_SP: ' \ t '
>>> print D2,type (D2)
<type ' str ' >
Python personal notes