- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 −
- raw_input
- input
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]
- Related Articles
- Reading data from keyboard using console class in Java
- How to close a Python figure by keyboard input using Matplotlib?
- Keyboard module in Python
- Single-Row Keyboard in python
- How to use the tag for keyboard input formatting in HTML?
- Program for adding 2 numbers input from keyboard in 8085 Microprocessor
- How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?
- Reading images using Python?
- Why would we call cin.clear() and cin.ignore() after reading input in C++?
- Reading and Writing Files in Python
- Keyboard shortcuts with Tkinter in Python 3
- Mouse and keyboard automation using Python?
- Reading and Writing to text files in Python
- Reading an image using Python OpenCv module
- Reading and Writing CSV File using Python

Advertisements