Two ways for Python to read keyboard input: two ways for python keyboard input
Python provides two built-in functions to read a line of text from the standard input. 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 (remove the line break at the end ):
Copy codeThe Code is as follows:
Str = raw_input ("Enter your input :");
Print "Received input is:", str
This will prompt you to enter any string and display the same string on the screen. When I enter "Hello Python! ", Its output is as follows:
Copy codeThe Code is as follows:
Enter your input: Hello Python
Written ed input is: Hello Python
Input Function
The input () function and raw_input () function can basically be exchanged, but the input assumes that your input is a valid Python expression and returns the calculation result. This should be the biggest difference between the two.
Copy codeThe Code is as follows:
Str = input ("Enter your input :");
Print "Received input is:", str
This will generate the following results corresponding to the input:
Copy codeThe Code is as follows:
Enter your input: [x * 5 for x in range (2, 10, 2)]
Recieved input is: [10, 20, 30, 40]