Summary: The key differences between
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
- 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.
Before looking at the differences between
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
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)
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
raw_input() functions. So let us jump into the mission-critical question:
Problem: What is difference between
input() in Python?
Let us have an in-depth look at each difference one by one:
|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|
◆ Since it accepts the input as it is, it does not expect the input to be syntactically correct.
◆ It expects a syntactically correct input (statement/expression) from the user.
|◆ In Python 3.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
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
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)
What is your name? Finxter Shubham Name: Finxter Shubham Enter a mathematical expression: 3+5 Output: 3+5
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”))
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)
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.
In this article, we discussed the key differences between
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
raw_input() have been clarified after reading this article.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.