2 ways for Python to read keyboard input

Source: Internet
Author: User

This article mainly introduces Python reading keyboard input 2 methods, mainly using the Raw_input function and input function, this article gives the use of examples, the need for friends can refer to the

Python provides two built-in functions for reading a line of text from standard input, and the default standard input is the keyboard. As follows:

1.raw_input

2.input

Raw_input function

The Raw_input () function reads a row from the standard input and returns a string (minus the ending line break):

The code is as follows:

str = raw_input ("Enter Your input:");

Print "Received input is:", str

This prompts you to enter any string and then displays the same string on the screen. When I type "Hello python!", its output is as follows:

The code is as follows:

Enter your Input:hello Python

Received input Is:hello Python

Input function

The input () function and the raw_input () function are basically interchangeable, but input assumes that your input is a valid Python expression and returns the result of the operation. This should be the biggest difference between the two.

The code is as follows:

str = input ("Enter your input:");

Print "Received input is:", str

This produces the following results for the input:

The code is as follows:

Enter your input: [x*5 for X in range (2,10,2)]

Recieved input is: [10, 20, 30, 40]

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.