Python input() Function

The input() function in python is used to take input from a user. It prompts a message to the user before taking the input value and after the user enters input, it returns the input as a string.

age = input('Enter your age: ')
print(age)

# Output
# Enter your age: 24
# 24

input() Function Syntax

The syntax of the input() function is given below:

input(message)

input() Function Parameters

The input() function takes only a single optional parameter:

  • message – (Optional) The message is prompted to the user before taking the input value.

input() Function Return Value

The input() function returns the input value entered by the user as a string.

This is to be noted that the input() function returns only and only a string value. This means, even if the user has entered an integer value it will convert it to a string.

input() Function Examples

Let’s try to understand the working of the input() function with the help of relevant examples.

Example 1: Take input from user and print it

In the example below, no message is prompted to the user before taking the input value.

Python input() function without prompt
# Take input with any messagae
name = input()

print('Your name is:', name)

Output:

John
Your name is: John

Example 2: Take input from user with a prompt

Asking an input from the user without prompting any message is a little bit confusing for the user. Therefore, in the example below, we have prompted a message ‘Enter your name’ to the user before taking the input.

Python input() Function with prompt
# Take input with a message
name = input('Enter your name: ')

print('Your name is:', name)

Output:

Enter your name: John
Your name is: John

Take integer input from user

As I already mentioned, the return type of the input() function is always a string. Then the question arises how can one take integer input from the user?

Well, it sounds tough but it isn’t. All you have to do is convert the user input from string to integer using the inbuilt int() function. See the example below:

Example:

age = input('Enter your age: ')
print('Type of age is: ', type(age))

age = int(input('Enter your age: '))
print('Type of age is: ', type(age))

Output:

Enter your age: 24
Type of age is:  <class 'str'>
Enter your age: 24
Type of age is:  <class 'int'>

Take Multiple inputs from User

A very common problem that Python developers generally face is how to take multiple inputs from a user at once. Well, there are several ways to accomplish it. But we are gonna learn only the most efficient and common methods that developers use.

1. Using multiple input() functions

The simplest way to take multiple inputs is to write the input() function multiple times. See the example below:

a, b = input('Enter a: '), input('Enter b: ')

print('a = ', a)
print('b = ', b)

Output:

Enter a: 10
Enter b: 20
a =  10
b =  20

2. Using split() function with input() function

This is the shortest way to take multiple inputs from a user. But here we can only show a single message to the user and he has to separate input values with a space. See the example below:

a, b = input('Enter Two Values: ').split()

print('a = ', a)
print('b = ', b)

Output:

Enter Two Values: 10 20
a =  10
b =  20

Take Multiple integer inputs from user

We have discussed how to take integer input from the user and also how to take multiple inputs from the user. Now, what if I want to take multiple integer inputs from a user in a single line. well, this is what we are gonna learn here.

As we discussed in the beginning of this tutorial that the input() function returns only and only string type. So how can we expect it to return integers? That’s right, it will return only string but at the time of taking the input, we will convert it to an integer. This is achieved by the map() function. See the example below:

a, b = map(int, input('Enter Two Values: ').split())

print('a = ', a, ' and type is: ', type(a))
print('b = ', b, ' and type is: ', type(b))

Output:

Enter Two Values: 10 20
a =  10  and type is:  <class 'int'>
b =  20  and type is:  <class 'int'>