Python input() Function


Python input() function

We use the Python input() function to ask the user to enter data. We could, for example, require the user to enter a name, age, phone number, etc.

Syntax : input(prompt)

Prompt : (optional argument) It is the string that is a default message to user before input.

Return : Returns the user input by converting it into a string.

Input is a function in python followed by a parenthesis. Inside the parenthesis, we specify the text that we want to print. Example, “Please enter the name”. It will prompt us to enter our name.

Example : input(“Please enter age:”)

In [1]:
input("Please enter name:")
Please enter name:Steve
Out[1]:
'Steve'
Python input() function
Python input() function

We can receive input from the user in some variables, which can be further used in different operations.

Example : a = input(“Please enter age:”)

Prompt is an optional argument here, we can use input() without prompt as well. We will look at an example below without using a prompt.

Receiving input in a variable.

In [1]:
first_name = input("Please enter first name:")
print("Hello!", first_name)
Please enter first name:Steve
Hello! Steve

Without prompt.

In [2]:
print("Please enter your name:")
name = input()
print("Hello!", name)
Please enter your name:
Chris
Hello! Chris

input() with typecasting

By default, the data type of value received by input is a string. We can convert it using type casting (which means converting the data type to other data types).

input() with typecasting
input() with typecasting

Here, we will first check the data type of the input using the type() function(In Python, type() function is used to check the data type of the variable.) and then, we will demonstrate an example where the user will input two numbers, and we will add them. We will save the values entered by the user in the variables. We have used the function int() for typecasting to convert string to integer for addition.

Check data type of input value.

In [1]:
a = input("Please enter first number : ")
b = input("Please enter second number : ")
print(type(a))
print(type(b))
Please enter first number : 5
Please enter second number : 6
<class 'str'>
<class 'str'>

input() with typecasting.

In [2]:
a = input("Please enter first number : ")
b = input("Please enter second number : ")
print("Sum =", int(a)+int(b))
Please enter first number : 5
Please enter second number : 6
Sum = 11

Nested input()

Input can also be nested inside print. In this case, Python will first execute the input function and then the print function. It will first ask the user to provide the input and then execute the print statement.

Nested input()
Nested input()
In [1]:
print("How are you " + input("Please enter name : ") + "?")
Please enter name : Steve
How are you Steve?

Multiple inputs in one line

We can ask multiple values from the user in one line. For example, in a single input, we can ask the user to enter id, name and age and save the information in the separate variables.

For this, we have to use the split function, and inside the split, we can pass the separator of our choice. For example, a comma is used as the separator in the below example.

In [1]:
emp_id, name, age = input("Please enter id, Full Name and age(separated by comma) : ").split(",")
print("ID: =", emp_id)
print("Name: =", name)
print("Age: =", age)
Please enter id, Full Name and age(separated by comma) : 02,Tim Gates,45
ID: = 02
Name: = Tim Gates
Age: = 45

Exercise: Ask user to input two numbers and subtract them.

In this example, we will ask the user to input two numbers and subtract the second from the first one. To complete this exercise, we have to convert the numbers into integers using the int() typecasting function. This conversion will be done on the same line by wrapping the input() in the int() function.

In [1]:
a = int(input("Please enter first number : "))
b = int(input("Please enter second number : "))
print("Result =", a - b)
Please enter first number : 6
Please enter second number : 5
Result = 1

Resources

Exercise QuestionHandwritten Notes

Expected Output

In [1]:
print("Sum =", int(input("Please enter first number : ")) + int(input("Please enter second number : ")))
Please enter first number : 10
Please enter second number : 10
Sum = 20

Note: All this has to be done in the single line.

References

  1. Input in Python
  2. Python input with examples
  3. Python Input Functions