The sys module is a common python module to handle system inputs and commands. sys.argv is the list of command line arguments that are passed into the Python program.

argv represents all the input that are entered into the command line. It is an array that hold the arguments input.

You need to import the sys module into your python program.

import sys

Next let’s assign a few variables to the input from sys.argv to validate and print the output to console.

Assign variable name to the 1st argument which is usually the program name. The first argument is store in the first items in the array/list (sys.argv[0]). Let’s assign that to the variable program_name.

program_name = sys.argv[0]

Next let’s assign the rest of the input list into another variable. other_arguments. sys.argv[1:] specific the 2nd argument till the end of the input.

other_arguments = sys.argv[1:]

It is useful to count the number of arguments after the program name in the event that you would like to do a for loop to display the input you can use the count for that purpose. The method to count the number of input after the program name in this tutorial is len()

count = len(other_arguments)

Now we can use the print function to see show the output in the console.

# print the number of arguments being input after the program name
print(“Number of Arguments after program name: “,count)

# print the variable type of program_name, by assigning the data in the list directly to a variable. The variable will be a string type.
print(“Type program_name”,type(program_name))

# print the variable type of other_arguments, by assigning the array or list to a variable. The type should be list
print(“Type other_arguments: “, type(other_arguments))

# to print the string we can just use the variable name directly and use + to concatenate to any string.
print(“Program Name: ” + program_name)

To print the list you can specific the position of the list directly or you can use a for loop to print out all the item in the list.

”’
to print the first item in the list you use the first array for other argument 0 is the first item in the newly assign variable.
However do take not by using the direct access option if you are trying to print an item that is null in the list you will get an error. A for loop is better at printing out the argument
”’
print(“First Argument in the list: ” + other_arguments[0])


”’
you can use the while loop to print all the items in the list using the count. you can use for loop to achieve this as well.
”’

arg=0 #initialize the arg to 0 which is the first item in the list
while arg < count:
print(other_arguments[arg]
)
#add 1 to the argument as it goes through the while loop and when the arg is greater than count it means the list is empty and it will exit the loop.
arg+=1

print(“Program Completed”)

Let’s take alook at the output.

#!/user/bin/python3
# Take input from command line arguments
import sys

#Assign the first arguments of the command line input which is usually the program name to program_name variable.
program_name = sys.argv[0]

#The rest of the arguments after the program are stored in a array. We assign it to the variable other_arguments
other_arguments = sys.argv[1:]

#Next we count the number of arguments in the input after the program_name
count = len(other_arguments)

#print the number of arguments being input after the program name
print(“Number of Arguments after program name: “,count)

#print the variable type of program_name, by assigning the data in the list directly to a variable. The variable will be a string type.
print(“Type program_name”,type(program_name))

#print the variable type of other_arguments, by assigning the array or list to a variable. The type should be list
print(“Type other_arguments: “, type(other_arguments))

#to print the string we can just use the variable name directly and use + to concatenate to any string.
print(“Program Name: ” + program_name)


”’
to print the first item in the list you use the first array for other argument 0 is the first item in the newly assign variable.
However do take not by using the direct access option if you are trying to print an item that is null in the list you will get an error. A for loop is better at printing out the argument
”’

print(“First Argument in the list: ” + other_arguments[0])
”’
you can use the while loop to print all the items in the list using the count. you can use for loop to achieve this as well.
”’
arg=0 #initialize the arg to 0 which is the first item in the list
while arg < count:
print(other_arguments[arg])
#add 1 to the argument as it goes through the while loop and when the arg is greater than count it means the list is empty and it will exit the loop.
arg+=1

print(“Program Completed”)

Hope you find this tutorial useful.