Command Line Arguments

When we are executing the python program in the command line, we have to pass python Here along with the file name, we can pass different values to the program, so these values are called command-line arguments.

>python value1 value2 ..
>python 10 20
  • is the file name (command line argument)
  • 10 is the value(command line argument)
  • 20 is the value(command line argument)

By default, python can have multiple command-line arguments that can be accessed through sys module, where sys is a default module that contains a variable called argv by using this we can access various types of command-line arguments.

The argv is List datatype, to understand this, let us consider below example

from sys import argv


In the case of command-line arguments in python, the first argument is always the python file name,

from sys import argv

I am going to save this file in a and execute; this program will print, which is the first argument(the first argument always has an index of zero). I execute the file

python 10 20 30





If you print(argv), then python will print all the list of arguments cmnd-line-argv-six

Next, consider another one example to print the number of arguments and the list of command-line arguments and also to print the command line arguments by one by.

from sys import argv
print("The number of command line arguments:", len(argv))
print("The list of command line arguments:",argv)
print("The command line argument one by one:")
for x in argv:


As you know before, by default python is going to consider file name as its first argument, and if we do not want to print the filename as the argument, by using the slicing operator, we can print only the explicitly given values as the arguments.

The following example to demonstrate By using the Slice operator to print the given values as the arguments.

#Program to print sum of given numbers provided as command line arguments:
from sys import argv
x=argv[1: ]
for y in args:
print('The sum:", sum)

If we try to execute the above code, then we are going to get an error because whatever values we are going to pass through the command-line argument is always considered as a String data type, so in case of sum= sum+y, x is considered as a string, and the sum is considered as a number, so whenever we try to add a string with an integer, immediately, we are going to get a syntax error.


E:Pythonpython codes>python
  File "", line 5
SyntaxError: invalid syntax

So, let us see how to correct it, instead of taking the only y, perform typecasting to convert the string into int type.

from sys import argv
x=argv[1: ]
for y in x:
print('The sum:',sum)


If we execute the above program without passing any arguments, then the sum will be 0.


Features of Command-Line Arguments

Space is the separator between the command line arguments if the argument itself contains the space, then it should be enclosed within the double-quotes.

from sys import argv

Here, I am going to execute this code through command prompt and passing the argument as king kong.


If you observe, python is considering the king as the first argument and prints the same, and if we want to print king kong as one argument only then we have to enclose this with double-quotes.

from sys import argv


And if we use single quotes, then the output will like below


Inside the python program command-line arguments available only in the form of the string, if we try to add two string, then it will result in concatenation operation and hence to overcome this problem we have to convert these string datatype into an int by typecasting

from sys import argv


If we try to access the command line argument with out of range argument, then Python will throw an Index error.

from sys import argv


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions