- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reading Keyboard Input in Python
Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are −
The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline).
#!/usr/bin/python str = raw_input("Enter your input: ") print "Received input is : ", str
This prompts you to enter any string and it would display same string on the screen. When I typed "Hello Python!", its output is like this −
Enter your input: Hello Python Received input is : Hello Python
The input Function
The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid Python expression and returns the evaluated result to you.
#!/usr/bin/python str = input("Enter your input: ") print "Received input is : ", str
This would produce the following result against the entered input −
Enter your input: [x*5 for x in range(2,10,2)] Recieved input is : [10, 20, 30, 40]
- Reading data from keyboard using console class in Java
- How to close a Python figure by keyboard input using Matplotlib?
- Keyboard module in Python
- Reading images using Python?
- Single-Row Keyboard in python
- Reading and Writing Files in Python
- Program for adding 2 numbers input from keyboard in 8085 Microprocessor
- Why would we call cin.clear() and cin.ignore() after reading input in C++?
- How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?
- Keyboard shortcuts with Tkinter in Python 3
- Reading and Writing to text files in Python
- Mouse and keyboard automation using Python?
- Reading an image using Python OpenCv module
- Reading and Writing CSV File using Python
- Reading and Writing to text files in Python Program