Introduction to Python Input Function
To interact with users, either to retrieve data or to showcase a notification/warning/result, you need a way out in the form of a reusable set of instructions, mostly referred to as functions. Python provides developers with a below-mentioned set of two built-in functions that can be used for reading the user fed input from the peripheral devices like keyboard etc.
Python Input Function
Below are the functions of Python Input:
- raw_input ( “Message/Output”)
- input ( “Message/Output”)
1. raw_input ( “Message/Output”)
This function is specific to the older version of python that is python 2.x
It reads the exact information typed by the user & stores it into the specific variable that we want to by converting that info into a string.
For Example:
Code:
# Raw_input syntax
g = raw_input("Enter your name : ")
print (g)
Output:
How Does This Function Work?
Once you run this piece of code, Python provides you with an interface to enter the text.
Let’s Enter “Python User” and hit Enter.
Once you hit Enter, Python will print the info entered by the user using the print function.
Let’s discuss the flow of this piece of code in detail.
Python executes the code in the same order as we write it in. So the very first line gets executed as soon as we run the cell.
Code:
g = raw_input("Enter your name : ")
It prints the text “Enter your name : ” to the console and provides a dialogue box for the user to enter the information.
This user-entered information is then converted into a string and stored in the variable ‘g.’
Let’s verify the same by checking out the type of variable ‘g’.
Code:
type(g)
Output:
As expected, the output is of the type “str.”
Coming back to where we left, the control is now available with the next line that is
print g
As expected, This prints the value of the variable ‘g’.
2. input ( “Message/Output”)
This function is specific to the newer version of python that is python 3.x and so on.
In Python 3.x, the raw_input() function has been altered with the input() function, so the only difference between these two functions is the Python version you are using it with.
It also serves the same purpose. Once the user enters the information, the input function evaluates the data type of that function and saves it to the specified variable.
Let’s try to understand this with the same example.
Code:
#input syntax
g = input("Please enter text")
print (g )
type(g)
Output:
Let’s now try using the raw_input()function with Python3.x to understand what happens.
Code:
# Raw_input usage with Python 3.x
g = raw_input("Please enter text")
Output:
As soon as we run this cell, Python throws the above error explicitly, specifying that the raw_input() function is not defined in Python 3.x.
How to Handle Numeric Value with Input() Function?
If you expect the user to enter a numeric value, you need to explicitly convert that into integer or float as per the requirement once the input function reads the info entered by the user as a string.
Let’s discuss the same with an example:
Code:
##Example to read numerical values
num = input(" Please enter your marks : ")
num = int(num)
type(num)
This can also be taken care of in a single line like by wrapping the input function within the int function like below:
Code:
num = int(input(" Please enter your marks : "))
type(num)
Output:
The order of operation of the above piece of code will be:
- First of all, the input function will put the program to a halt unless the user enters a value and hit enter
- As soon as enter is hit, the control is passed back to the program
- The info entered by the user and is converted into a string
- This string is passed to the int() function and is converted into an integer, which is then saved to the variable “num.”
- Then the type function verifies the data type of the variable “num.”
Let’s discuss one more example that provides the product of two numbers entered by the user.
Code:
num1 = int(input("Please enter num1: "))
num2 = int(input("Please enter num2: "))
print(num1 * num2)
Suppose the user enters num1 = 2 and num 2 = 3; as per the above input, the output will be 6.
Output:
What if you expect a float Instead of an Integer?
In case you want to convert the user fed input to float, then all you need to do is use the float()function along with the input() function like below:
Code:
mks1 = int(input("Please enter your marks in paper 1: "))
mks2 = int(input("Please enter your marks in paper 2: "))
print("Your total marks are")
print(mks1 + mks2)
print("your percentage is")
print((mks1+mks2)*100/200)
Output:
What if the user enters invalid input, that a character instead of numerals?
Suppose the user enters num1 = 3 and num 2 = e
Python will throw an error in this case about the int() function.
Conclusion
To read user-specified information in python you can use input() function with the newer versions of python (Like Python 3.x) &raw_input() function with the older version of Python (Like Python 2.x) input() &raw_input() function serve the same purpose. The difference is only in the version of Python you are using these functions with input() &raw_input() function converts the user-specified input into a string before saving it to the desired variable. You can use int() or float() function if you intend to read numerical values along with input() function
Recommended Articles
This is a guide to Python Input Function. Here we discuss a brief overview of Python Input Function and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses