Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.
- raw_input ( prompt )
- input ( prompt )
raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example –
Here, g is a variable which will get the string value, typed by user during the execution of program. Typing of data for the
raw_input() function is terminated by enter key. We can use
raw_input() to enter numeric data also. In that case we use typecasting.For more details on typecasting refer this.
input ( ) : This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by python. For example –
How the input function works in Python :
input()function executes program flow will be stopped until the user has given an input.
- The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional.
- Whatever you enter as input, input function convert it into a string. if you enter an integer value still
input()function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting.
- Taking input from console in Python
- Taking multiple inputs from user in Python
- Vulnerability in input() function – Python 2.x
- Python | Get a list as input from user
- Python Input Methods for Competitive Programming
- How to input multiple values from user in one line in Python?
- Python | Find all close matches of input string from a list
- Generate two output strings depending upon occurrence of character in input string in Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- bin() in Python
- SHA in Python
- set add() in python
- zip() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.