Command Line Arguments in Python | Python Web Development

Command Line Arguments in Python | Python Web Development

Python command-line arguments - everything you should know. Get to know sys.argv & getopt module that helps you parse command-line options and arguments.

Python command-line arguments provide a user-friendly interface to your normal text-based command-line program, same as other graphical user interfaces for applications.

Command-line Argument Uses

Till now we are using the raw input or input method to take input from the command line but this works only while the program/script is running. But what we are going to discuss is how we are going to run any python program/script from the command line which accepts input with it.

Most Used Module:

  • sys.argv
  • getopt module


It provides functions and variables that are used to manipulate the parts of the Python runtime environment. This module gives access to the variables which are used or maintained by the interpreter and function which interact with the interpreter. Some sample examples are:

  1. List of command-line arguments (sys.argv).
  2. Number of Command line arguments (len(sys.argv)).
  3. Provides name of current script (sys.argv[0]).


Note: Write the below code in a python file as sys argv work only in script file

import sys

n = len(sys.argv)

print("Total arguments passed:", n)

print("\nName of Python script:", sys.argv[0])

print("\nArguments passed:", end = " ")

for i in range(1, n):

    print(sys.argv[i], end = " ")

Using argparse module :

Sum = 0

for i in range(1, n):

    Sum += int(sys.argv[i])

print("\n\nResult:", Sum)

Command to be executed in terminal:

 $ python3 1 3 6 8 9

python web development program/script

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[]( "")**, our full-stack Python development services...

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[]( "")**, we constantly endeavor to give you exactly what you need. If you need to...

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.