Python raw_input() vs input()

Summary: The key differences between raw_input() and input() functions are the following:

  • raw_input() can be used only in Python 2.x and is obsolete in Python 3.x and above and has been renamed input()
  • In Python 2.x, raw_input() returns a string whereas input() returns result of an evaluation. While in Python 3.x input() returns a string but can be converted to another type like a number.

Overview

Before looking at the differences between raw_input() and input(), let us understand why we need them!

A user-friendly code is one that is interactive. To make a code interactive instead of hard coding values, a developer/programmer must aim to allow the user to input their own values into the program. We use the raw_input() and input() functions to accept user inputs.

Example: The following program is an example to accept user input in Python:

name = input("Please enter your full name: ")
age = input("Please enter your age: ")
# In Python2.x use raw_input() instead

print("Name: ", name)
print("Age: ", age)

Output

Please enter your full name: FINXTER
Please enter your age: 25
Name:  FINXTER
Age:  25

In this article, we shall be discussing the key differences between the input() and raw_input() functions. So let us jump into the mission-critical question:

Problem: What is difference between raw_input() and input() in Python?

Let us have an in-depth look at each difference one by one:

Existential Difference

raw_input()input()
Inbuilt function present only in Python 2.x and is not a part of Python 3.xInbuilt function present in both, Python 2.x and Python 3.x

Functional Difference Based on Python Versions

 Python 2.xPython 3.x
raw_input() raw_input() accepts input as it is, i.e. exactly as the input has been entered by the user and returns a string.

◆ Since it accepts the input as it is, it does not expect the input to be syntactically correct.  
raw_input() is obsolete and no longer a part of Python 3.x and above.
input() input() accepts the input from the user as a statement or expression and returns the output after evaluating the input. In other words, it accepts the user entry as raw_input(), performs an eval() on it, and then returns the result as output.

◆ It expects a syntactically correct input (statement/expression) from the user.
◆ In Python 3.x, raw_input() has been replaced by input(). This means that the input() function performs the same operation in Python 3.x as raw_input() used to do in Python 2.  

Thus input() accepts and returns a string in Python 3.x and above.   

Examples

Python 2.x

input() function

a = raw_input("What is your name? ")
print "Name: %s" %a)
b = raw_input(" Enter a mathematical expression: ")
print Output":  %d", %b

Output

What is your name? Finxter
Name:  Finxter
 Enter a mathematical expression: 2+5
Output:  2+5

raw_input() function

a = input("Enter Your Full Name: ")
print "Name: %s " %a
b = input("Enter a Mathematical Expression: ")
print "Output: %d" %b

Output

Enter Your Full Name: 'Finxter Shubham'
Name: Finxter Shubham
Enter a Mathematical Expression: 5**2
Output: 25

Python 3.x And Above

input() function

a = input("What is your name? ")
print("Name: ", a)
b = input("Enter a mathematical expression: ")
print("Output: ", b)

Output

What is your name? Finxter Shubham
Name:  Finxter Shubham
Enter a mathematical expression: 3+5
Output:  3+5

Trivia

If you want to implement or leverage the functionality of input() of Python 2.x in Python 3.x and evaluate the statement entered by the user, you can use one of the following procedures:

  • Type Conversion : int(input(“Enter value”))
  • Using eval(input(“Enter Value”))

Example

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Addition: ", a+b)
x = eval(input("Enter a mathematical expression: "))
print("Result: ", x)

Output:

Enter first number: 25
Enter second number: 75
Addition:  100
Enter a mathematical expression: 10**2
Result:  100

But you must avoid the usage of eval() function unless necessary because it has a severe drawback.

I would strongly recommend you to read this article in connection with this topic. It will help you have a broader understanding of this concept. Also, if you are wondering about the version of python installed in your system, you may want to have a look at this article.

Conclusion

In this article, we discussed the key differences between input() and raw_input() in terms of their functionality and existence in different versions of Python along with their examples. I hope all your doubts regarding the difference between input() and raw_input() have been clarified after reading this article.

Please stay tuned and Subscribe for more interesting articles!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!