sys.argv is a list of command-line arguments as strings. When you run your program via the command line, the format is as follows: python PROGRAM_NAME.py arg1 arg2 arg3 ... Again, sys.argv is a list of command-line arguments, so, according to the line of code above, sys.argv[0] will be PROGRAM_NAME.py, sys.argv[1] will be arg1 (as a string), and so on. What you could do is this: import sys def quadratic(): args = sys.argv a = float(args[1]) b = float(args[2]) c = float(args[3]) s1 = (-b + (b**2 - 4*a*c)**0.5)/(2*a) s2 = (-b - (b**2 - 4*a*c)**0.5)/(2*a) print("Your solutions are ", s1, "and", s2) quadratic() Assuming your program is called quadratic.py , one could type into the command line python quadratic.py 3 2 -1 And the program would output the solution to 3x^2 + 2x - 1 = 0. Everything I said can be found here . Answer from HowToBakePie on reddit.com
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
The elements of sys.orig_argv are the arguments to the Python interpreter, while the elements of sys.argv are the arguments to the user’s program.
🌐
Reddit
reddit.com › r/learnpython › looking for a clear explanation on how to use sys.argv?
r/learnpython on Reddit: Looking for a clear explanation on how to use sys.argv?
April 6, 2021 -

Our instructor wants us to to use sys.argv[] to turn in programs to the auto-grader.

Here's an example of a (very simple) program I've written:

def quadratic():
    a = float(input("What is a? "))
    b = float(input("What is b? "))
    c = float(input("What is c? "))
        
    s1 = (-b + (b**2 - 4*a*c)**0.5)/(2*a)
    s2 = (-b - (b**2 - 4*a*c)**0.5)/(2*a)

    print("Your solutions are ", s1, "and", s2)

It's the quadratic formula. Can someone show me how to implement sys.argv to this code, just as an example?

I've googled several sites and watched a few YouTube videos, but the explanations made zero sense to me. Can someone break it down for me?

Thanks.

Top answer
1 of 9
425

I would like to note that previous answers made many assumptions about the user's knowledge. This answer attempts to answer the question at a more tutorial level.

For every invocation of Python, sys.argv is automatically a list of strings representing the arguments (as separated by spaces) on the command-line. The name comes from the C programming convention in which argv and argc represent the command line arguments.

You'll want to learn more about lists and strings as you're familiarizing yourself with Python, but in the meantime, here are a few things to know.

You can simply create a script that prints the arguments as they're represented. It also prints the number of arguments, using the len function on the list.

from __future__ import print_function
import sys
print(sys.argv, len(sys.argv))

The script requires Python 2.6 or later. If you call this script print_args.py, you can invoke it with different arguments to see what happens.

> python print_args.py
['print_args.py'] 1

> python print_args.py foo and bar
['print_args.py', 'foo', 'and', 'bar'] 4

> python print_args.py "foo and bar"
['print_args.py', 'foo and bar'] 2

> python print_args.py "foo and bar" and baz
['print_args.py', 'foo and bar', 'and', 'baz'] 4

As you can see, the command-line arguments include the script name but not the interpreter name. In this sense, Python treats the script as the executable. If you need to know the name of the executable (python in this case), you can use sys.executable.

You can see from the examples that it is possible to receive arguments that do contain spaces if the user invoked the script with arguments encapsulated in quotes, so what you get is the list of arguments as supplied by the user.

Now in your Python code, you can use this list of strings as input to your program. Since lists are indexed by zero-based integers, you can get the individual items using the list[0] syntax. For example, to get the script name:

script_name = sys.argv[0] # this will always work.

Although interesting, you rarely need to know your script name. To get the first argument after the script for a filename, you could do the following:

filename = sys.argv[1]

This is a very common usage, but note that it will fail with an IndexError if no argument was supplied.

Also, Python lets you reference a slice of a list, so to get another list of just the user-supplied arguments (but without the script name), you can do

user_args = sys.argv[1:] # get everything after the script name

Additionally, Python allows you to assign a sequence of items (including lists) to variable names. So if you expect the user to always supply two arguments, you can assign those arguments (as strings) to two variables:

user_args = sys.argv[1:]
fun, games = user_args # len(user_args) had better be 2

So, to answer your specific question, sys.argv[1] represents the first command-line argument (as a string) supplied to the script in question. It will not prompt for input, but it will fail with an IndexError if no arguments are supplied on the command-line following the script name.

2 of 9
38

sys.argv[1] contains the first command line argument passed to your script.

For example, if your script is named hello.py and you issue:

$ python3.1 hello.py foo

or:

$ chmod +x hello.py  # make script executable
$ ./hello.py foo

Your script will print:

Hello there foo
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-sys-argv-in-python
How to use sys.argv in Python - GeeksforGeeks
December 8, 2025 - In Python, sys.argv is used to handle command-line arguments passed to a script during execution.
🌐
Codecademy
codecademy.com › article › command-line-arguments-in-python
Command Line Arguments in Python (sys.argv, argparse) | Codecademy
Python provides three main ways to handle command-line arguments: sys.argv: A simple way to access command-line arguments as a list of strings.
🌐
National Instruments
forums.ni.com › community › discussion forums › most active software boards › ni teststand › python system arguments not passed (argv empty)
Python system arguments not passed (argv empty) - NI Community
June 9, 2020 - As a workaround, when TestStand calls your module, you can set sys.argv[0] and then continue the operation. This is the same workaround you need to when when you import the module in a python REPL and execute the function.
🌐
Reddit
reddit.com › r/learnpython › what does argv do exactly and how/when do you use it (in the simplest terms it can be put)?
r/learnpython on Reddit: What does argv do exactly and how/when do you use it (In the simplest terms it can be put)?
February 8, 2019 - Then the filename will be stored in sys.argv[1] and your code can do whatever with it. It’s an alternative to hardcoding filenames into a script. It also becomes handy when you use argparse because now a user who is running a script can set ...
Find elsewhere
🌐
PythonForBeginners
pythonforbeginners.com › home › command line arguments in python
Command Line Arguments in Python - PythonForBeginners.com
June 11, 2023 - In a Python program, sys.argv is the list of command line arguments passed to the program while executing it through the command line interface.
🌐
freeCodeCamp
forum.freecodecamp.org › python
Python sys.argv - Python - The freeCodeCamp Forum
April 22, 2022 - I want to take arguments with sys and do some operations but it is not clear how many arguments to write so how can I get all of them can I use *
🌐
Python.org
discuss.python.org › documentation
Sys.argv command line parsing (documentation) - Documentation - Discussions on Python.org
October 18, 2024 - As I understand it, on Windows python.exe gets a command line string, which it parses into command line arguments. Since sys.argv documentation doesn’t define a parsing method, I guess that argv parsing is both undefine…
🌐
Medium
pavolkutaj.medium.com › the-simplest-introduction-to-sys-argv-as-a-way-to-pass-arguments-from-command-line-3e9e63761bc5
The Simplest Introduction To Sys.Argv As A Way To Pass Arguments From Command Line | by Pavol Z. Kutaj | Medium
February 22, 2022 - These are notes under the amazing course Core Python: Getting Started ... at the bottom of your file, use the pattern to run script immediatelly if called from a shell (if __name__ == "__main__":...) use argv[<n>] to refer to the first, second, and nth positional argument · the number must start from 1 not from 0 for example: # md2med.pyimport sys def main(doc_name, file_to_publish): # CODE ACCEPTING THE PARAMS HERE #... if __name__ == "__main__": main(doc_name=sys.argv[1], file_to_publish=sys.argv[2])
🌐
Medium
orichh.medium.com › what-is-pythons-sys-argv-0-1573754a633d
what is python’s sys.argv[0]?
October 30, 2020 - If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string. argv stands for ‘argument vector’ within ...
🌐
Scaler
scaler.com › home › topics › command line arguments in python
Command Line Arguments in Python - Scaler Topics
January 16, 2024 - The sys.argv variable is considered to be the easy and useful list structure that can help to read the command-line arguments as a string by implementing the command-line arguments via the sys.argv list structure where the list element is single ...
🌐
Hongtaoh
hongtaoh.com › en › 2020 › 10 › 05 › python-sys-argv
What is sys.argv in Python and How to Use It
October 5, 2020 - This variable contains arguments that are to be passed to the python script you are calling in the command line1. I’ll explain by example. First, take a look at nyt_state_data.py . It has three sys.argv variables:
🌐
LinkedIn
linkedin.com › pulse › understanding-sysargv-python-harshvardhan-pandey
Understanding sys.argv in python
October 9, 2019 - sys .argv will display the command line args passed when running a script or you can say sys.argv will store the command line arguments passed in python while running from terminal
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-main.html
Python main()
The main() above begins with a CS106A standard line args = sys.argv[1:] which sets up a list named args to contain the command line arg strings. This line works and you can always use it.
🌐
Byucs110
fall2023.byucs110.org › guide › unit4 › using-sys.argv
Best practices for using sys.argv - CS 110 How to Program
Since this is the first block of code that is run when a script is run, it’s best practice to only reference sys.argv inside of this block. First, let’s show a bad example of a file, bad_example.py, that references sys.argv outside of the if __name__ == "__main__" block:
🌐
Python.org
discuss.python.org › python help
Docs library "sys.argv" - Python Help - Discussions on Python.org
January 19, 2026 - in my code: 1 import sys 2 print(“hello, my name is”, sys.argiv[1]) is printing out an error. what should i do to fix that code. i want it have a prompt to fill ….