Python Scripting (9 Blogs) Become a Certified Professional

Command Line Arguments in Python

Last updated on May 22,2019 12.5K Views


myMock Interview Service for Real Tech Jobs


myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Python supports the creation of programs that can be run on the command line, completely with command-line arguments. It provides a getopt module, with which command line options and arguments can be parsed.

Accessing Command Line Arguments

The Python sys module provides access to any of the command-line arguments via sys.argv. It solves two purposes:

  • sys.argv is the list of command line arguments
  • len(sys.argv) is the number of command line arguments that you have in your command line
  • sys.argv[0] is the program, i.e. script name

Executing Python

You can execute Python in this way:

$python inp1, inp2 inp3



import sys

print ‘Number of arguments:’, len (sys.argv), ‘arguments.’

print ‘Argument List:’, str(sys.argv)

It will produce the following output:

Number of arguments: 4 arguments.

Argument List: [‘’, ‘inp1’, ‘inp2’, ‘inp3’]

Note: One thing to remember here is that indentation and spacing is extremely important, otherwise it may give error or incorrect number of arguments.

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Python 101: Hello World Program

Why should you go for Python?

Introduction to Strings in Python

Start learning Python for Big Data Analytics


Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.