Python Input
Python Input
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.
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()
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