How to get user input in python 2 and 3?
In python 2 there are two ways to get user input whereas in python 3 there is only one way to do.
Let me explain each separately in the below with example each. In the end, you will be able to tell the difference between getting user input in python 2 and 3.
User Input in python 2
In Python 2 we have two methods to get user input. One of the ways is using the raw_input() function. This method will get string user input. Whatever you give the input as it will take as a string. For example,
name = raw_input("Enter your name: ")
Another way to get the user input in python 2 is by using input() function. It will take user input based on the type of input you give. If you have given input as an integer value, it will take as an integer. If you give input as float value it will take input as a float value. But it will never take a string value. It will give you an error unless you do the typecasting. For example,
age = input("Enter your age: ")
If you give input as 12 the type of age will be an integer, if you give input as 12.5 its type will be float and if you have given input as twelve it will give an error.
User Input in python 3
In Python 3 we have only one method to get user input. The raw_input() is not there in python 3. But we can get all kind of user input in python 3 too. The function that we use is input(). By default, it will take a string as an input. But don't worry we can use eval() function along with input() function to take user input based on the type of input the user gives. Similar to the input() function of python 2. You can also do typecasting without using eval() function if you know what kind of user input you need. For example,
name = input("Enter your name:")
age = eval(input("Enter your age"))
In the first example, It will take input as a string whether you give string value, integer value or float value. Whereas in the second example, the input depends on the type of input you give. If you give 12 as input the age will be an integer, if you give 12.5 as input the age will be float type and if you give twelve as input the age will be a string type.
TypeCasting
You might be wondering what typecasting is if you are new to programming. Because I have been saying typecasting from beginning only. Basically, typecasting is converting one type of data into another. Typecasting can be done efficiently if you typecast small data type to bigger because you will not lose precision. Like short to integer or float and an integer or float to long double. For example,
age=int(input())
or
age = input()
age=int(age)
Both ways do the same work. In the first method, we convert the data during the time of getting input. Whereas in the second method, we get the age as a string and then we convert it into an integer.
Tips: When you get user input using the above method, both in python 2 and 3 you will be able to get only one input per line. What if you want to get more than one input in a single line. The answer is simple, follow the way it is shown below.
Suppose you want to get 'n' integer value in a single line, do the following.
In Python 2:
age = list(map(int, raw_input().split()))
In Python 3:
age = list(map(int, input().split()))
In both the cases what we do is we take input as a string and then we split it and store it as an integer in a list. Hope it will be some help to my dear viewers and reader.