User Input from Keyboard – input() function in Python
- input() function
- Syntax of input() function
- Getting User Input using input() function
- Getting the type of entered input value
- Getting integer as User input
- Getting float as User Input
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
1)input() function
- The input() built-in method in Python can read user input from the keyboard.
- The user’s input is read as a string, which can then be assigned to a variable.
- We must press the “Enter” button after entering the value from the keyboard. The user’s value is then read by the input() function.
- The program will pause indefinitely while waiting for user input. There is no way to specify a timeout value.
- The EOFError is produced and the application is ended if we type EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return).
2)Syntax of input() function
The input() function has the following syntax:
input(prompt)
On the console, the prompt string is printed, and the user is given control to enter the value. Print some helpful information to assist the user in entering the expected value.
3)Getting User Input using input() function
Here’s an example of getting user input and printing it to the console.
Below is the implementation:
value = input("Enter the value ") print("the value of entered input = ", value)
Output:
Enter the value BTechGeeks the value of entered input = BTechGeeks
4)Getting the type of entered input value
The value given by the user is always transformed to a string before being allocated to a variable. To be sure, let’s use the type() method to determine the type of the input variable.
Below is the implementation:
value = input("Enter the value which is of type string ") print("the type of entered input value = ", type(value)) value1 = input("Enter the value which is of type integer ") print("the type of entered input value = ", type(value1))
Output:
Enter the value which is of type string BTechGeeks the type of entered input value = <class 'str'> Enter the value which is of type integer 356 the type of entered input value = <class 'str'>
5)Getting integer as User input
There is no method to acquire a user input of an integer or any other kind. However, we can convert the entered string to an integer using the built-in functions.
Below is the implementation:
value = input("Enter the value ") intValue = int(value) print("the type of entered input value = ", type(intValue))
Output:
Enter the value 4312 the type of entered input value = <class 'int'>
6)Getting float as User Input
There is no method to acquire a user input of an float or any other kind. However, we can convert the entered string to an float using the built-in functions.
Below is the implementation:
value = input("Enter the value ") floatValue = float(value) print("the type of entered input value = ", type(floatValue))
Output:
Enter the value 4561823.198 the type of entered input value = <class 'float'>
In Python, using the input() function to get user input is fairly simple. It’s primarily used to give the user an option of operation and then change the program’s flow accordingly.
Related Programs:
- Python Program to Return Multiple Values From a Function
- Python Program to Read a String from the User and Append it into a File
- Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it
- Harshad Number in Python – Easy Implementation
- Implementing Sentinel Search in Python – Easy Explanation
- Linear Search in Python – A Practical Approach
- Python program to Convert Kilometers to Miles and Vice Versa – A Step-By-Step Approach