Difference between Input() and raw_input() functions in Python
There are two input functions in Python that are used for taking input are given below:
- raw_input()
- input()
What is raw_input() Function?
raw_input() function is a built-in function in Python. It is used to get the values or to receive from the user.
The return type of the raw_input() function is in the form of string type or STR. This input function is applicable for or used only in Python 2. x version.
Example:
Let’s understand it by taking an example is below:
# raw_input() function in Python 2.x
x = raw_input("enter 1st input: ")
y = raw_input("enter 2nd input: ")
z = raw_input("enter 3rd input: ")
# print the value of the input function
print "value of 1st input: ", x
#print the type of input value
print "type of 1st input: ", type(x)
print "value of 2nd input: ", y
print "type of 2nd input: ", type(y)
print "value of 3rd input: ", z
print "type of 3rd input: ", type(z)
Input:
5
"javatpoint"
6.79
Output:
value of 1st input: 5
type of 1st input: <type 'str'>
value of 2nd input: "javatpoint"
type of 2nd input: <type 'str'>
value of 3rd input: 6.79
type of 3rd input: <type 'str'>
This is the example of the raw_input() function in Python 2. x. In this, the return type of this function is only string type. Because of this, if we enter the value 5, then the return type is string only, so if we want to convert it into an integer, then we have to type cast it. And in the above example, we give input as 6.79, and then the return type is string only, so if we want to convert it into a float, then we have to type cast it.
In this, we are required to perform typecasting explicitly.
Example
Let’s understand typecasting by taking the example is below:
Example by using typecasting:
# raw_input() function in Python 2.x
# typecasting in raw_input() function
x = int(raw_input("enter 1st input: "))
y = raw_input("enter 2nd input: ")
z = float(raw_input("enter 3rd input: "))
print "value of 1st input: ", x
print "type of 1st input: ", type(x)
print "value of 2nd input: ", y
print "type of 2nd input: ", type(y)
print "value of 3rd input: ", z
print "type of 3rd input: ", type(z)
Input:
5
"javatpoint"
6.79
Output:
value of 1st input: 5
type of 1st input: <type 'int'>
value of 2nd input: "javatpoint"
type of 2nd input: <type 'str'>
value of 3rd input: 6.79
type of 3rd input: <type 'float'>
The typecasting return type of 5 is 'int', and the return type of 6.79 is 'float'.
What is Input() Function?
The input() function takes values from the user and informs the programme to wait for the user to enter the values. It is a built-in function in Python. This input function is applicable for or used in Python 2. x and 3. x versions.
Let’s first discuss about input() function in Python 2 .x .
In Python 2. x, the input() function takes the value from the user, and the return type is based on the user's provided value (if you enter the value as an int type, then the return type itself will be 'int’).
Example:
# input() function in Python 2.x
x = input("enter 1st input: ")
y = input("enter 2nd input: ")
z = input("enter 3rd input: ")
#print the value of input value
print "value of 1st input: ", x
print "type of 1st input: ", type(x)
print "value of 2nd input: ", y
print "type of 2nd input: ", type(y)
print "value of 3rd input: ",z
print "type of 3rd input: ", type(z)
Input:
5
"javatpoint"
6.79
Output:
value of 1st input: 5
type of 1st input: <type 'int'>
value of 2nd input: javatpoint
type of 2nd input: <type 'str'>
value of 3rd input: 6.79
type of 3rd input: <type 'float'
In this, we are not required to perform typecasting explicitly.
The drawback of using input function in python 2. x:
As we know, in python 2. x, we can also input a variable using the input function. This makes our program less secure. To understand it, let's take an example.
Suppose we make a log-in page and its back-end in python 2. x, for log-in user inputs his user_name and password.
user_name = "test_name@231"
password = "test_password231"
name_input = input("Enter username: ")
password_input = input("Enter password: ")
# if user_name is equal to name_input and
#password is equal to password_input. Only then can the user log in.
if user_name == name_input and password == password_input:
print "logged in successfully"
else:
print "wrong user name or password"
At user’s side:
>> Enter username: "name@453"
>> Enter password: "3@stak*s"
>> wrong user name or password
It seems everything is working fine for invalid user names and password users cannot log in. But if the user inputs user_name and password.
At user’s side:
>> Enter username: user_name
>> Enter password: password
>> logged in successfully
Here, a user can log in without a correct username and password. So the input function is less secure. If you are using the python 2. x version, it is not recommended to use the input function and the raw_input function that returns string function and typecast if needed.
let’s see above example with raw_input finction
user_name = "test_name@231"
password = "test_password231"
name_input = raw_input("Enter username: ")
password_input = raw_input("Enter password: ")
# if user_name is equal to name_input and
#password is equal to password_input. Only then the user can log in.
if user_name == name_input and password == password_input:
print "logged in successfully"
else:
print "wrong user name or password"
At user’s side:
>> Enter username: user_name
>> Enter password: password
>> wrong user name or password
Here you can see that users cannot log in after giving input user_name and password, so it is more secure than the input function in python 2. x.
input() function in Python 3.x :
In python 3.x ,we have only one input function that is input() function.
input() function in Python 3.x is not same as input() function in Python 2 .x.
In python 3.x input function work similar to the raw_input() function. It converts all inputs into a string of STR and returns it. But in python 2. x, its working is different. If we use an input function in python 2. x, it does not convert inputs into the string. It takes value with its data type.
Example: Let’s understand it by taking an example is below:
x = input("Enter the name of the student : ")
print(x)
print(type(x))
# for typecasting
y = int(input("Enter the marks of the student : "))
print(y)
print(type(y))
Input:
Ram
100
Output:
Enter the name of the student: Ram
Ram
<class 'str'>
Enter the marks of the student: 100
100
<class 'int'>
In this, we are required to perform typecasting explicitly.
Example 2: Let’s understand this typecasting by taking another example:
x=input("Enter first number : ")
y=input("Enter second number : ")
sum = x+y
# concatination of two strings
print(sum)
print(type(sum))
# by using type casting
x=int(input("Enter first number : "))
y=int(input("Enter second number : "))
sum = x+y
# sum of two numbers
print(sum)
print(type(sum))
Input:
1
2
Output:
Enter first number : 1
Enter second number : 2
12
<class 'str'>
Enter first number : 1
Enter second number : 2
3
<class 'int'>
In the above example, it is shown that firstly, we take two numbers by input() function and perform a sum operation, then it will concatenate it like a string because the return type of input() function is a string. After this, we use typecasting, then we take two numbers by input() function and perform a sum operation. Then, it will sum it like an integer because we have used typecasting here.