How to take Multiple Inputs in Python?

How to take Multiple Inputs in Python?

Python is a popular programming language used for a wide range of applications, from data analysis to web development. One of the essential skills that every Python programmer should master is taking multiple inputs. Whether you’re creating a command-line tool or building a web application, being able to accept multiple inputs from the user is critical. In this blog post, we’ll discuss how to take multiple inputs in Python.

There are several ways to take multiple inputs in Python, but the most common ones are using the input() function, sys.argv, and argparse module.

Also Read: Software Development Services in Singapore

Using the input() function:

The input() function is a built-in function in Python that allows the user to enter input from the keyboard. It accepts a single argument, which is an optional prompt string. The prompt string is displayed to the user before waiting for input. To take multiple inputs using the input() function, you can split the input string into a list using the split() function.

Here’s an example:

scss
input_string = input("Enter multiple values separated by space: ")
values_list = input_string.split()
print(values_list)

In the example above, we’re using the input() function to get input from the user. We’re also providing an optional prompt string, “Enter multiple values separated by space: “. After getting the input, we’re splitting the input string into a list using the split() function. Finally, we’re printing the list of values.

Using sys.argv:

The sys.argv is a list in Python that contains the command-line arguments passed to the Python script. The first item in the list is always the name of the script, and the rest of the items are the arguments. To take multiple inputs using sys.argv, you can pass the values as command-line arguments when running the script.

Here’s an example:

scss

import sys

values_list = sys.argv[1:]
print(values_list)

In the example above, we’re importing the sys module and getting the command-line arguments using the sys.argv list. We’re using slicing to get all the items in the list except the first item, which is the script name. Finally, we’re printing the list of values.

To run the script and pass command-line arguments, you can use the following command in the terminal:

python script.py value1 value2 value3

Using the argparse module:

The argparse module is a powerful module in Python that allows you to create command-line interfaces with ease. It makes it easy to write user-friendly command-line interfaces and parse command-line arguments. To use the argparse module, you need to create an instance of the ArgumentParser class and define the arguments you want to accept.

Read More: CRM System Singapore

Here’s an example:

scss

import argparse

parser = argparse.ArgumentParser()
parser.add_argument(“values”, nargs=”+”)
args = parser.parse_args()

print(args.values)

In the example above, we’re importing the argparse module and creating an instance of the ArgumentParser class. We’re defining a positional argument called “values” using the add_argument() method. The “nargs=+” argument tells argparse to accept one or more values. Finally, we’re using the parse_args() method to parse the arguments and printing the list of values.

To run the script and pass command-line arguments, you can use the following command in the terminal:

python script.py value1 value2 value3

Conclusion:

Taking multiple inputs in Python is an essential skill that every Python programmer should master. There are several ways to take multiple inputs in Python, but the most common ones are using the input() function, sys.argv, and argparse module. The input() function is the simplest way to get input from the user, but it has some limitations. The sys.argv method is useful for simple command-line interfaces, but it requires the user to remember the order of the arguments. The argparse module is the most powerful method and allows you to create user-friendly command-line interfaces, but it requires a bit more setup.