Join Regular Classroom : Visit ClassroomTech

How to take multiple inputs in one line / single line in Python

There are various methods of taking multiple inputs in a single line.

Method 1:

One of the method is split() method. This methods splits the input separated by separator.

Syntax:

input().split(separator)

Example 1:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# for taking two inputs
# using split() method

x, y = input().split()
print("First: ", x)
print("Second: ", y) 

Input:

4 5

Output:

First:  4
Second:  5

Explanation:

Here the two inputs are split into two integers and assigned to two variables (x, y).

Example 2:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# for taking three inputs
# using split() method

x, y, z = input().split()
print("First: ", x)
print("Second: ", y)
print("Third: ", z) 

Input:

4 5 8

Output:

First:  4
Second:  5
Third:  8

Explanation:

Here the three inputs are split into three integers and assigned to three variables (x, y, z).

Example 3:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using split() method

x = input().split()
print(x)

Input:

4 5 8

Output:

['4', '5', '8']

Explanation:

Here the three inputs are split into three integers and assigned to a single variable (x) as strings inside a list.
Note: Here the individual inputs are a string type. We cannot type cast it as Integer here.

Method 2:

Using the map() function. map() returns an object so it needs to be converted into a list or tuple. This method maps the input to multiple integer or single integer according to the requirements.

Syntax:

variable = list(map(data_type, input().split(separator)))

Example 1:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using map() function

x = list(map(int, input().split()))
print(x)

Input:

4 5 8

Output:

[4, 5, 8]

Explanation:

Here it mapped all the integers to a single variable (x) as integers only inside a list.

Example 2:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using map() function
# It can also take input as a string

x = list(map(str, input().split()))
print(x)

Input:

code hustle repeat

Output:

['code', 'hustle', 'repeat']

Explanation:

Here all the characters separated by as space are returned to a single list as strings.

Example 3:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using map() function

x = list(map(int, str(int(input()))))
print(x)

Input:

456789

Output:

[4, 5, 6, 7, 8, 9]

Explanation:

Here it returned all single digit variables without any separator to a list as six different integers.

Method 3:

Using list comprehension.

Example 1:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using list comprehension

x = [int(x) for x in input()]
print(x)

Input:

456

Output:

[4, 5, 6]

Explanation:

Here the loop returns all single digit variables without any separator to a single list as three different integers.

Example 2:

# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using list comprehension
# for assigning it to multiple variables

x, y, z = [int(x) for x in input()]
print("First: ",x)
print("Second: ",y)
print("Third: ",z)

Input:

456

Output:

First:  4
Second:  5
Third:  6

Explanation:

Here the loop returns all single digit variables without any separator to three different variables as three different integers.


You Missed

Also Checkout