Differences between input () and raw_input () in python

Source: Internet
Author: User
This article mainly introduces the differences between input () and raw_input () in python. For more information, see the source code of input.

Def input (prompt ):
Return eval (raw_input (prompt ))

Actually, the input also calls raw_input, but implements eval processing.

What is the role of eval?

Input: converts data types based on user input.
Raw_input: the user input is processed as a string.

Below is a detailed supplement:

First, we know that both input () and raw_input () are used to obtain the input from the console. of course, the input prompt information can be added during input:

        a = raw_input("Please input a:")        b = input("Please input b:")

So what is the difference between the two?

Input () allows you to enter numbers or expressions. input strings are not supported and numeric values are returned. Raw_input () captures the original input, that is, a string is returned. Therefore, if the input is a number, we must perform a forced conversion. For example:

   a = int(raw_input("Please input the number a:"))

In fact, input () is implemented by raw_input () in essence. it only calls the eval () function after raw_input () is called. Therefore, unless there is a special need for input, otherwise, we recommend that you use raw_input () to interact with users.


Map receives a function and an iteratable object (such as a list) as a parameter, processes each element with the function, and then returns a new list.
ACM sometimes needs to input a row in the format of a B c, and then it uses the map function for processing. Here we assume that a, B, and c are integers.
A, B, c = map (int, raw_input (). split (). The raw_input function inputs a string. the string split method is used to divide the string into sequences.

For more information about the differences between input () and raw_input () in python, see PHP!

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.