[Python] Read command-line arguments with sys.argv

Created on November 30, 2026

sys.argv is a list of strings containing the script name followed by its command-line arguments. If you pass an argument, you can read the first one at index 1.

Save the following code as get-commandline-args.py.

import sys

args = sys.argv
print(args[1])
python3 get-commandline-args.py hello
hello

If the argument is optional, check that the list has at least two entries before accessing args[1].

Python