How to use sys.argv in Python Last Updated : 03 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, sys.argv is used to handle command-line arguments passed to a script during execution. These arguments are stored in a list-like object, where the first element (sys.argv[0]) is the name of the script itself and the rest are arguments provided by the user.This feature is helpful when you want to make your Python script interactive with input provided at runtime from the command line. What is the sys Module?The sys module in Python provides access to some variables used or maintained by the interpreter. It includes tools to interact with the runtime environment and sys.argv is one such variable used to handle command-line arguments.Examples of sys.argvExample 1: Basic Use of sys.argv Python import sys print("This is the name of the program:", sys.argv[0]) print("Argument List:", str(sys.argv)) Output:Explanation:sys.argv[0] returns the name of the script.str(sys.argv) prints the list of all command-line arguments including the script name.Use the following command to run the script:python script_name.py arg1 arg2Commonly Used Functions with sys.argv Python import sys print("This is the name of the program:", sys.argv[0]) print("Number of elements including the name of the program:", len(sys.argv)) print("Number of elements excluding the name of the program:", len(sys.argv) - 1) print("Argument List:", str(sys.argv)) Output:Explanation:len(sys.argv) gives the total number of command-line arguments.len(sys.argv) - 1 gives the number of user-passed arguments excluding the script name.str() helps to display the list as a readable string.Example 2: Adding Numbers Using Command Line Arguments Python import sys add = 0.0 # Get the number of command-line arguments n = len(sys.argv) # Start from index 1 to skip the script name for i in range(1, n): add += float(sys.argv[i]) print("The sum is:", add) Output:Explanation:This script accepts numeric inputs via command-line arguments.Converts them to float and adds them.Indexing starts from 1 to skip the script name.Use the following command to run the script:python script_name.py 3.5 4.2 2.3Related articles: sys module Comment More infoAdvertise with us Next Article How to get the current username in Python V vanshikagoyal43 Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads How to get the current username in Python When building interacting Python programs you might need to get the current username for features like personalizing user experience, providing user-specific resources, Implementing robust logging and debugging, strengthening security, etc. We will use different Python modules and functions like os. 4 min read How to run bash script in Python? If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python 2 min read Python | os.system() method The OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system-dependent functionality.os.system()Â method executes the command (a string) in a subshell. This method 3 min read How to Install python-dotenv in Python Python-dotenv is a Python package that is used to manage & store environment variables. It reads key-value pairs from the â.envâ file.How to install python-dotenv?To install python-dotenv using PIP, open your command prompt or terminal and type in this command.pip install python-dotenvVerifying 3 min read sys.stdout.write in Python sys.stdout.write() is a built-in Python method that writes output directly to the console without automatically adding a newline (\n). It is part of the sys module and requires import sys before use. Unlike print(), it does not insert spaces between multiple arguments, allowing precise control over 3 min read How To Embed Python Code In Batch Script Embedding Python code in a batch script can be very helpful for automating tasks that require both shell commands and Python scripting. In this article, we will discuss how to embed Python code within a batch script. What are Batch Scripts and Python Code?Batch scripts are text files containing a se 4 min read Like