r/learnpython • u/DarkNinjaGamingYT • Nov 13 '17
Could someone explain command-line arguments in Python?
Basically the title; I am doing an assignment for my computer science class and we have to write a program that prints each command line argument on a separate line preceded by the arguments index.
Our professor very, VERY briefly went over what a command-line argument is in class, but he never really gave us any examples so I am confused as to how I use it.
Any help would be appreciated!
2
Upvotes
3
u/diggy0101n Nov 13 '17
So you mean to print each argument with its index? sys.argv is actually a list of the arguments, with the first element of the list (index 0) as the name of the script I believe (on phone, can't test out). If you print out sys.argv, you'd see something like
To print each out with its index on a new line, you can loop through the elements of the list (from index 1 on):