input() Function and its Use in Python Program
Input and Output in Python
In this lesson, we will understand what input() function is and how it works in Python programming with the help of some examples.
What is input() Function
The input() function is used in the Python program to accept input from the standard input device (keyboard) and store them in variable. The input() is similar to print() function. Instead of printing data on the output device (monitor), it reads data entered from the input device (keyboard).
Let's see some examples for more understanding.
Example 1
Python program to input a name and display it on the screen.
n=input('Enter your name: ')
print('Hello',n)
Output
Enter your name: Peter Hello Peter
Here you can see that we have used the input() function to take input from the user. You can also see that within the ( ) of input function we have written the text Enter your name this message will be displayed on the screen when you run the program.
The message in input() function is optional if don't want to display any message just don't write any message in the ( ) of the input function. Now after taking input from the user we have stored the input in variable n. After that on line number 2 we have printed the name on the screen using the print() function.
Example 2
Python program to input an integer and a float number and store it in variables.
print('Enter an integer and a float number')
a=int(input())
b=float(input())
print('You have entered an integer number',a,'and a float number',b)
Output
Enter an integer and a float number 15 24.153 You have entered an integer number 15 and a float number 24.153
The input() function reads input from the user and converts it into the string. To convert the input into an integer or float we have to use explicit typecasting as discussed earlier in Typecasting lesson. So we have used int() function and float() function to convert the input into an integer and float respectively. After storing the values in variable a and b, we have used the print() function to print the stored values on the screen.
Example 3
Python program to take multiple inputs in a single line.
print('Enter roll, name and percentage')
r,n,p=int(input()),input(),float(input())
print('Roll:',r)
print('Name:',n)
print('Percentage:',p)
Output
Enter roll, name and percentage 1 Amit 87.36 Roll: 1 Name: Amit Percentage: 87.36
Here you can see that on line number 2 we have used multiple input() functions in a single line by using comma (,) to input an integer, a string and a float value in variables r, n and p respectively. After that, we have used print() function to print the stored value on the screen.
Use of type() function
type() function is used to check the type of value stored in a variable. Let's see an example of type() function.
Example (Use of type() function)
Python program to show the use of type() function.
a='apple'
b=56
c=18.23
d=True
print(type(a));
print(type(b));
print(type(c));
print(type(d));
Output
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Here you can see that using type() function we have printed the type of values stored in the variables a, b, c and d respectively on the screen.