Python

raw_input python 3 vs Python 2 Compatible or not Compatible ?

Well, raw_input python 3 is not valid. The raw_input() works only in Python 2.x. Actually, This function enables python to wait for user input from the keyboard. But In place of raw_input(), There is also an input function in Python 3. Which works in the same way. In this article, We will see how raw_input function works in Python?

Compatibility of raw_input python 3 :

As I have already mentioned, The above function is the only compatible with Python 2.x. let’s see what happens If we use it in Python 3.

var=raw_input("input")
print(var)

When we run this code in Python 3 Interpreter We get the below error.

 

 

Alternative of raw_input() in Python 3.x-

We may use input() function in python 3 as an alternative to raw_input.  Let’s understand with some examples.

var=input("input")
print(var)
input python

Difference between input() in Python 2 & Python 3 –

In python 3 if we use the input function it converts everything into str object by default. Let’s see some real examples.

var=input("input")
print(type(var))
input function python 3 default type

As the above example shows we have entered an integer which converts it into str implicitly. But On the opposite side,  python 2 input () never changes the user input type. If a user input an integer, It will be an integer (No object typecasting ).

raw_input() typecasting object:

raw_input() converts every object into str by default. It is quite the same as in python 3 input (). So Please take while accessing it in python code. Because usually, we think its type will remain unchanged. And it breaks the code in further flow.

Conclusion –

raw_input python function is really very useful in day to day code development. Especially as a Python beginner, It is the first thing the developers learn. Well, now I hope you understood the uses of raw_input(). Still, if you have any doubt please comment in the comment box.

Thanks

Data Science Learner Team