Python raw_input () and input () functions read Interactive input

Source: Internet
Author: User


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:hellotraceback (most recent call last):  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 <typ E ' str ' >

7.2. When the input is a pure number:
>>> B1 = Raw_input ("raw_input_int:") raw_input_int:123>>> print B1,type (B1) 123 <type ' str ' > >>> B2 = input ("Input_int:") input_int:123>>> print B2,type (b2) 123 <type ' int ' >

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 ' >&G t;>> 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: \ttraceback (most recent call last):  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 raw_input () and input () functions read Interactive input

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.