The difference between raw_input () and input ()

Source: Internet
Author: User
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.

  • 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.