Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python raw_input() vs input()
1
Python raw_input() vs input()

Summary: The key differences between
Code:
raw_input()
and
Code:
input()
functions are :
  • Code:
    raw_input()
    can be used only in Python 2.x and is obsolete in python 3.x and above and has been renamed
    Code:
    input()

  • In Python 2.x,
    Code:
    raw_input()
    returns a string whereas
    Code:
    input()
    returns result of an evaluation. While in Python 3.x
    Code:
    input()
    returns a string but can be converted to another type like a number.

Overview

Before looking at the differences between
Code:
raw_input()
and
Code:
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
Code:
raw_input()
and
Code:
input()
functions to accept user inputs.

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

Code:
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

Code:
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
Code:
input()
and
Code:
raw_input()
functions. So let us jump into the mission-critical question:

Problem: What is difference between
Code:
raw_input()
and
Code:
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.x |
Inbuilt function present in both, Python 2.x and Python 3.x

Functional Difference Based on Python Versions

  |
Python 2.x |
Python 3.x

raw_input() |
◆
Code:
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.   |
◆
Code:
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,
Code:
raw_input()
has been replaced byinput(). This means that the input() function performs the same operation in Python 3.x as
Code:
raw_input()
used to do in Python 2.  

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

Examples

Python 2.x

✎ input() function

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

Output

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

✎ raw_input() function

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

Output

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

Python 3.x And Above

✎ input() function

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

Output

Code:
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
Code:
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

Code:
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:

Code:
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
Code:
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 atthis article.

Conclusion

In this article, we discussed the key differences between
Code:
input()
and
Code:
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
Code:
input()
and
Code:
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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: