Python User Input from Keyboard – input() function

In this tutorial, you will learn about Python’s User Input function and how to take user input from the keyboard using Python.

  • The input() built-in function in Python allows you to read keyboard input.
  • A string representation of the user’s input is obtained, and it can then be assigned to a variable.
  • Pressing “Enter” after keying in the value completes the process. Then, the input() function reads the value that the user has entered.
  • Until the user provides input, the program pauses indefinitely Providing a timeout value isn’t an option.
  • There will be an EOF error and the program will be terminated if we type EOF (*nix: Ctrl-D, Windows: Ctl-Z+Return).

Syntax of input() Function in Python 2.x and Python 3.x

The syntax of input() function for Python 2.x is:

raw_input(prompt)

where: prompt = String representing a sentence or words.

The Syntax of input() function for Python 3.x is:

input(prompt)

where: prompt = String representing a sentence or words.

The user is given the option to enter a value into the console, and the prompt string is displayed. User guidance is needed in order for the expected value to be entered.

How To Get User Input in Python

An example of printing the user’s input to the console is shown here.

#Taking a User input as Name
name = input("Enter Your Name\n")

#Printing the Entered Name
print("Hi! {}".format(name))

Output:

Enter Your Name
Coduber
Hi! Coduber
Python User Input from Keyboard – input() function

What is the type of value entered by the user?

The value entered by the user is always converted to a string before being assigned to the variable. Let’s check this by using the type() function to determine the type of the input variable.

#Taking a User input as Anything
name = input("Enter Anything A Number or String\n")

#Printing the Type of Entry Made
print("The Entered Type is {}".format(type(name)))

Output:

Enter Anything A Number or String
12
The Entered Type is <class 'str'>
Python User Input from Keyboard – input() function

How To Take String Input In Python

If you are using a Python version greater than or equal to 3, you can directly use the input() function, which will convert all input from the user into string input, regardless of the type of input received. As illustrated in the above example.

#Taking a User input as Anything
name = input("Enter Anything A Number or String\n")

#Printing the Type of Entry Made
print("The Entered Type is {}".format(type(name)))

In addition, if you are using a Python version that is lower than 3, for example, a Python version 2.x, you will need to make use of the raw input() function, which converts all user input into a string and stores it in a local variable.

How to get an Integer as the User Input in Python?

After entering a string into the input function, you will need to typecast that string in order to convert it into an integer. Before typecasting, use the isdigit() function to verify that the input contains only digits.

In the below example I am also handling the error if done during user input in Python.

#Asking user to enter the input as Number.
number = input("Enter Anything A Number\n")

#Make sure the input is digits and represent number
if(number.isnumeric()):
    #Typecasting the Number to Integer
    number = int(number)
    print("You can entered the number {} and the type is {}".format(number, type(number)))
else:
    print("Input is not a Number.")

Output:

Enter Anything A Number
10
You can entered the number 10 and the type is <class 'int'>
Python User Input from Keyboard – input() function

Python User Input Multiple Choice Example

Providing users with a choice and relying on their input to guide them through the choice is how we can build an intelligent system.

Here is an example of the output generated by the above-mentioned program during its execution.

#Asking user to enter the input as Number.
number1 = int(input("Enter first number\n"))
number2 = int(input("Enter second number\n"))

choice = int(input("Enter 1 to Add and 2 to Substract the Number\n"))

if choice == 1:
    print("The addition of two number is {}".format(number1 + number2))
elif choice == 2:
    print("The substraction of two number is {}".format(number1 - number2))
else:
    print("You Entered Incorrect Choice.\n")

Output:

Enter first number
10
Enter second number
20
Enter 1 to Add and 2 to Substract the Number
1
The addition of two number is 30
Python User Input from Keyboard – input() function

What Does The Python Input() Function Do

As you already saw in the above examples the input function in Python takes input from the user and then saves it to a given variable.

Using the optional argument of raw input([arg]), input() presents a prompt to the user and receives user input. User-input is returned in a format that can be parsed by Python 2.x.

While “Hello” is stored as a string, the user’s entry of “5” will be treated as an integer. When using Python 3.x, the data input by the user is returned as a string.

Wrap Up

The input() function in Python makes it very simple to take user input. It’s mostly used to give the user a choice of operation and then change the program’s flow accordingly.

The program, on the other hand, waits indefinitely for user input. It would have been nice to have a timeout and a default value if the user did not enter the value in time.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Fix: Python Is Not Set From Command Line Or npm Configuration
  2. How To Take Integer Input In Python 3
  3. How To Compare String and Integer in Python
  4. How To Print Fractional Part In Python

Leave a Comment