raw_input python 3 vs Python 2 Compatible or not Compatible ?

raw_input python 3 featured image

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.


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.

input python
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.

input function python 3 default type
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.


Data Science Learner Team



Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner