09-13-2020, 12:48 AM
Python raw_input() vs input()
Summary: The key differences between and functions are :
Overview
Before looking at the differences between and , 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 and functions to accept user inputs.
Example: The following program is an example to accept user input in Python:
Output
In this article, we shall be discussing the key differences between the and functions. So let us jump into the mission-critical question:
Problem: What is difference between and 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() |
◆ 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. |
◆ 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, has been replaced byinput(). This means that the input() function performs the same operation in Python 3.x as used to do in Python 2.
Thus accepts and returns a string in Python 3.x and above.
Examples
Python 2.x
✎ input() function
Output
✎ raw_input() function
Output
Python 3.x And Above
✎ input() function
Output
Trivia
If you want to implement or leverage the functionality of of Python 2.x in Python 3.x and evaluate the statement entered by the user, you can use one of the following procedures:
Example
Output:
But you must avoid the usage of 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 betweenand 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 and 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!
Summary: The key differences between
Code:
raw_input()Code:
input()- can be used only in Python 2.x and is obsolete in python 3.x and above and has been renamedCode:
raw_input()Code:input()
- In Python 2.x, returns a string whereasCode:
raw_input()returns result of an evaluation. While in Python 3.xCode:input()returns a string but can be converted to another type like a number.Code:input()
Overview
Before looking at the differences between
Code:
raw_input()Code:
input()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()Code:
input()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: 25In this article, we shall be discussing the key differences between the
Code:
input()Code:
raw_input()Problem: What is difference between
Code:
raw_input()Code:
input()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()◆ Since it accepts the input as it is, it does not expect the input to be syntactically correct. |
◆
Code:
raw_input()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()Code:
raw_input()Thus
Code:
input()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", %bOutput
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" %bOutput
Code:
Enter Your Full Name: 'Finxter Shubham'
Name: Finxter Shubham
Enter a Mathematical Expression: 5**2
Output: 25Python 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+5If you want to implement or leverage the functionality of
Code:
input()- 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: 100But you must avoid the usage of
Code:
eval()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()Code:
raw_input()Code:
input()Code:
raw_input()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!

