/  Technology   /  Difference between Raw Input and Input functions in Python
Difference between Raw Input and Input functions in Python

Difference between Raw Input and Input functions in Python

 

An application often has to be interactive with the end-user. To do this, the program has to take in some data from the user. Python does this task using two built-in functions input() and raw_input(). Now, what’s the necessity for two different functions for doing the same operation?

 

Well, the difference is seen syntactically and more precisely, when we use these in Python2 and Python3. Let’s unfold this difference step by step.

 

Input()

We use this built-in function in both the versions i.e. Python2 and Python3. It is used to let the program know that it has to wait for the user input to proceed further in its execution.

 

In Python 3: the input() function takes the data from the user, converts it irrespective of the type and returns the type string. This means that python doesn’t evaluate the entered data, all it does is, consider the entered data a string, even if it’s not an actual string or int or any type.

 

In Python 2: the input() function takes in the user input and returns it as it is without altering the type of it i.e. Python evaluates the entered data and then returns the respective evaluated type.

 

#input() in Python 3

a = input("Enter your name: ")
# printing the type of input value
print(type(a))
print(a)

b = input("Enter a number: ")
print(type(b))

#changing the type of b from string to int
b = int(b)
print(type(b))
print(b)

Output:

>>Enter your name: i2tutorials
‘str’
i2tutorials
>>Enter a number: 12
‘str’
‘int’

 

# input() function in Python2

a = input("Enter your name: ")
print(type(a))


print(a)

b = input("Enter a number: ")
print(type(b))
print(b)

Output:

>>Enter your name: i2tutorials
‘str’
>>Enter a number: 12
‘int’

 

raw_input()

This built-in function is used to let the program know that it has to wait for the user input to proceed further in the execution. We use this only in Python2. Its functionality is similar to that of input() in Python3.

 

Or we can say they renamed raw_input() in Python2 as input() in Python3.

 

Program Code:

# raw_input() function in Python2

a = raw_input("Enter your name: ")
print(type(a))

print(a)

b = raw_input("Enter a number: ")
print(type(b))

#changing the type of b from string to int
b = int(b)
print(type(b))
print(b)

Output

>>Enter your name:i2tutorials
‘str’
i2tutorials
>>Enter a number:12
‘str’
‘int’
12

 

Leave a comment