How to Get User Input in Python from Keyboard?
We already know, Programming is all about processing data. To process the data program needs information as input. It can come in many ways.
- input from the database
- information from external resources like machine/device/website
- by typing on the keyboard
- hovering or clicking by mouse
In this post, we are only interested in getting keyboard input from the user.
Here in this post, we check out, how to receive input from the keyboard. And what is the difference between ‘raw_input()’ and input() function?
Python 2: Getting command line input using ‘raw_input()’
There is function raw_input(). The name itself depicts as it read raw data. It read user data as a string. It does not have the intelligence to interpret the type of data; the user has entered.
name = raw_input('Enter your keyboard input:')
All the data until you press the return key, will be read by this function.
By default, raw_input reads the input data and gives it in the form of a string.
Note: It is an in-built function, so you don’t need to import any external library.
Python 3: Keyboard input from the user using ‘input()’
It provides in build function ‘input()’ to read the input from the user.
name = input("What's your Good name? ")
input() reads the data from the user as a string and interprets data according to the type of data entered by the user.
If the user enters an integer/float value, Python reads it as a integer/float, unlike raw_input() function from Python 2.
Note: You can also use
input() method in Python 2. If you use the
input() function in Python 2, it always read the data as a string and you have to convert it according to your need.
Let’s start exploring it further.
Difference between input and raw_input function
If you want to get input as integer or float in Python 2, you need to convert data into int or float after reading data using raw_input().
mode=int(raw_input('How old are you?:'))
As we are taking input from the user, we can not ensure if the given user input value type is a numeric data type (float or int) or not.
So it is good practice to catch the error using try and except.
try: mode=int(raw_input('Keyboard Input:')) except ValueError: print("This is not a number.)
Another way is you can check the data type of the input using
mode = raw_input('Keyboard Input:') if(mode.isdigit()): print("Number is an integer.") else: print("Number is not an integer.")
Handling error is good practice to prevent the code from crashing at runtime.
How to Get User Input in Python for Both Python versions?
But now real confusion starts from here.
What about if you do not know the python version to whom you are going to run your code? Or do you want the code to run on both Python versions?
It’s simple. Just check the version of the python and then put the if-else statement to distinguish the Python input function as per the Python version.
Suppose if you are running code on Python 2.x, it should use the raw_input() function to read user value. If the same is going to run on Python 3.x environment, it should use the input() function.
So the first task is to find the Python version number.
To get the Python version, you need to import
Use the following code snippet to read user input for both Python versions.
from sys import version_info python_3 = version_info #creates Boolean value #for a test that Python major version if python_3==3: response = input("Please enter your Good Name: ") else: response = raw_input("Please enter your Good Name: ")
- Python 2.x has raw_input() function to get keyboard input. Likewise, Python 3.x has function input().
input()the function has more intelligence to interpret the user input datatype than
- You can use the input() in both Python 2 and Python 3 versions.
- The input() function in Python 2 takes every input as a string. Whereas same function in Python 3, convert the input into the appropriate data type.
- If you want your code to work on both Python versions, use condition by checking its python version.
Hope you got the solution, how to Get User Input in Python for both Python 2 and Python 3 version. If you have any questions, feel free to ask me in a comment.