Taking multiple inputs from user in Python

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to learn how to take multiple inputs from the user in Python.

The data entered by the user will be in the string format. So, we can use the split() method to divide the user entered data.

Let's take the multiple strings from the user.

Example

# taking the input from the user
strings = input("Enter multiple names space-separated:- ")
# spliting the data
strings = strings.split()
# printing the data
print(strings)

Output

If you run the above code, then you will get the following result.

Enter multiple names space-separated:- Python JavaScript Django React
['Python', 'JavaScript', 'Django', 'React']

What if we want to take multiple numbers as input? We can convert each input to an integer using the map and int function. Let's see an example.

Example

# taking the input from the user
numbers = input("Enter multiple numbers space-separated:- ")
# spliting the data and converting each string number to int
numbers = list(map(int, numbers.split()))
# printing the data
print(numbers)

Output

If you run the above code, then you will get the following result.

Enter multiple numbers space-separated:- 1 2 3 4 5
[1, 2, 3, 4, 5]

Conclusion

You can modify the code according to your needs and take the input from the users. If you queries in the tutorial, mention them in the comment section.

raja
Published on 11-Jul-2020 08:16:35
Advertisements