What is the difference between input() and raw_input() in Python

Nowadays, Developers get some data from the user for multiple reasons. however, there are basically two ways to get data from the user.

  • raw_input()
  • input()

These are the inbuilt functionalities provided by Python to get user data.

In Python, find out the difference between the input() and raw_input()

Now let’s see the difference between that in Python 2.x,

  • First and foremost, is that raw_input() always returns output in a string form only even we give a number as an input. If we want to get an integer as an output then we have to use typecasting. for instance,
x = raw_input("Enter Numbers: ")
print(type(x))
y = int(x)
print(type(y))

Output:

String

int
  • While input() provides whatever type of value we give as an input. For example, if the user inputs int value then it will return an integer value if the value is in string formate then it will return in string formate, and so on.
x = input("Enter Number: ")
print(type(x))

Output:

int

Note: Here, output depends on what type of number is given if it is a float then returns float data type and for same with integers.

 

Now lets’s see the changes between input() and raw_input() in Python 3.x,

Here, we have only the input function, meaning there are no functions like raw_input(). But, the behavior of input() is the same as raw_input().

So, it will always return a string regardless of the input the user gives. for example,

x = ("Enter Number: ")
print(type(x))

Output:

Str

Same as raw_input(), if we want to change the type then we have to use typecasting explicitly like,

x= int(input("Enter any Number: ")

Now, this will return an integer value.

Leave a Reply

Your email address will not be published. Required fields are marked *