Getopt module in Python Last Updated : 26 Jun, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The getopt module is a parser for command-line options based on the convention established by the Unix getopt() function. It is generally used for parsing an argument sequence, such as sys.argv.This module helps scripts parse command-line arguments in sys.argv. It works similarly to the C getopt() function for parsing command-line parameters.Syntaxgetopt.getopt(args, options, [long_options])Parameters:args: List of arguments to be passed.options: A string of option letters that the script wants to recognize. Options that require an argument should be followed by a colon (:).long_options: A list of strings containing the names of long options. Options that require arguments should be followed by an equal sign (=).Return Type: returns a tuple consisting of two elements:A list of (option, value) pairs.A list of program arguments left after the option list was stripped.Example 1: Using Short-Form OptionsParses short options -f and -l for first and last names. Python import sys import getopt def full_name(): first_name = None last_name = None argv = sys.argv[1:] try: opts, args = getopt.getopt(argv, "f:l:") except: print("Error") for opt, arg in opts: if opt == '-f': first_name = arg elif opt == '-l': last_name = arg print(first_name + " " + last_name) full_name() Output:Explanation: In this example, the function full_name() prints the full name by accepting the first name and last name from the command line, where 'f' is used for the first name and 'l' for the last name.Example 2: Using Long-Form OptionsNow lets look into the case where instead of short-form like 'f' or 'l', we could use full forms as 'first_name' and 'last_name'.The below code uses full forms to print full name Python import sys import getopt def full_name(): first_name = None last_name = None argv = sys.argv[1:] try: opts, args = getopt.getopt(argv, "f:l:", ["first_name=", "last_name="]) except: print("Error") for opt, arg in opts: if opt in ('-f', '--first_name'): first_name = arg elif opt in ('-l', '--last_name'): last_name = arg print(first_name + " " + last_name) full_name() Output:Note: Use single dash (-) for short options (e.g., -f) and double dash (--) for long options (e.g., --first_name). Long options requiring arguments must end with = without spaces. Comment More infoAdvertise with us Next Article External Modules in Python R RajuKumar19 Follow Improve Article Tags : Python Write From Home python-modules Practice Tags : python Similar Reads Docopt module in Python Docopt is a command line interface description module. It helps you define a interface for a command-line application and generates parser for it. The interface message in docopt is a formalized help message. Installation You can install docopt module in various ways, pip is one of the best ways to 3 min read Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st 3 min read External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m 5 min read Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there 4 min read Python datetime module In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python 14 min read Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext 9 min read Like