Python User Input from Keyboard – input() function

Python User Input from Keyboard – input() function

User Input from Keyboard – input() function in Python

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: