0% found this document useful (0 votes)
3 views

Python Input

The document explains how to take multiple inputs in Python using four methods: the split() function, the input() function, the map() function, and list comprehension. Each method includes syntax examples and sample code demonstrating how to capture user input effectively. These techniques allow users to gather multiple values in a single line, accommodating various data types.

Uploaded by

blackitachi2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Input

The document explains how to take multiple inputs in Python using four methods: the split() function, the input() function, the map() function, and list comprehension. Each method includes syntax examples and sample code demonstrating how to capture user input effectively. These techniques allow users to gather multiple values in a single line, accommodating various data types.

Uploaded by

blackitachi2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming in Python

Topic : How to take multiple inputs in a single line

a) Using split() function


b) Using input () function
c) Using map () function
d) Using List Comprehension

a) split ()

split() function helps us get multiple inputs from the user and assign them to the respective
variables in one line. This function is generally used to separate a given string into several
substrings. However, you can also use it for taking multiple inputs. The function generally
breaks the given input by the specified separator and in case the separator is not provided
then any white space is considered as a separator.

The syntax for using the split function to take multiple inputs is

variable 1, variable 2 = input("Enter what has to be conveyed to the user here"). split() # for
space-separated inputs

variable 1, variable 2 =input("Enter what has to be conveyed to the user here"). split(" , ") # for
comma separated inputs

An example is,
# Python program to take multiple inputs from the user
a, b = input("Enter two of your lucky number: ").split()
print("First lucky number is: ", a)
print("Second lucky number is: ", b)

Output:
Enter two of your lucky number: 7 1
First lucky number is: 7
Second lucky number is: 1
b) input ()

You can take multiple inputs in one single line by using the raw_input function several times as
shown below.

#multiple inputs in Python using input


x, y = input("Enter First Name: "), input("Enter Last Name: ")
print("First Name is: ", x)
print("Second Name is: ", y)

Output:
Enter First Name: FACE
Enter Last Name: Prep
First Name is: FACE
Second Name is: Prep

c) map()

map() is another function that will help us take multiple inputs from the user. In general, the
syntax for map function is map (fun, iter). Here fun is the function to which the map function
passes each iterable.
Syntax for multiple input using map()

variable 1, variable 2, variable 3 = map(int,input().split())


An example to take integer input from the user.

#multiple inputs in Python using map


x, y = map(int, input("Enter two values: ").split())
print("First Number is: ", x)
print("Second Number is: ", y)
Output:
Enter two values: 7 1
First Number is: 7
Second Number is: 1

An example to take string input from the user.


#multiple inputs in Python using map
x, y = map(str, input("Enter your first and last name ").split())
print("First Name is: ", x)
print("Second Name is: ", y)

d) List Comprehension

List data types also helps in taking multiple inputs from the user at a time. The syntax for
creating a list and storing the input in it is

x, y=[x for x in input("Enter two value: ").split()]


List allows you to take multiple inputs of different data type at a time. The below example will
help you understand better.

#multiple inputs in Python using list comprehension


x, y = [x for x in input("Enter your name and age: ").split(",")]
print("Your name is: ", x)
print("Your age is: ", y)
Output:
Enter your name and age: FACE Prep, 8
Your name is: FACE Prep
Your age is: 8

You might also like