Python Tutorial
Python - Introduction Python - Syntax Python - Variable Python - Number Python - Casting Python - String Python - Operators Python - Conditions Python - Loops Python - List Python - Tuple Python - Set Python - Dictionary Python - Function Python - Module Python - Date & Time Python - Input Output Python - Error & Exception Python - File Handling
Python OOP
OOP - Introduction OOP - Class Members OOP - Constructor Destructor OOP - Data Hiding OOP - Inheritance OOP - Overriding OOP - Overloading

Python Input Output

There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities.

Command Line Arguments

The Python sys module provides access to any command-line arguments via the sys.argv.

This serves two purposes:

  • sys.argv is the list of command-line arguments.
  • len(sys.argv) is the number of command-line arguments.

Now run above script in terminal as follows:

First argument is always script name and it is also being counted in number of arguments.

Python provides a getopt module that helps you parse command-line options and arguments.

This module provides two functions and an exception to enable command line argument parsing.

The getopt.getopt method parses command line options and parameter list.

Following is simple syntax for this method:

getopt.getopt(args, options, [long_options])

Here is the detail of the parameters:

  • args - This is the argument list to be parsed.
  • options - This is the string of option letters that the script wants to recognize, with options that require an argument should be followed by a colon (:).
  • long_options - This is optional parameter and if specified, must be a list of strings with the names of the long options, which should be supported. Long options, which require an argument should be followed by an equal sign ('='). To accept only long options, options should be an empty string.
  • This method returns value consisting of two elements: the first is a list of (option, value) pairs. The second is the list of program arguments left after the option list was stripped.
  • Each option-and-value pair returned has the option as its first element, prefixed with a hyphen for short options (e.g., '-x') or two hyphens for long options (e.g., '--long-option').

Now run above script in terminal as follows:


The string format() method formats the given string into a nicer output in Python.

The syntax of format() method is:

template.format(p0, p1, ..., k0=v0, k1=v1, ...)

Here, p0, p1,... are positional arguments and, k0, k1,... are keyword arguments with values v0, v1,... respectively.

And, template is a mixture of format codes with placeholders for the arguments.

String format() Parameters

format() method takes any number of parameters. But, is divided into two types of parameters:

  • Positional parameters - list of parameters that can be accessed with index of parameter inside curly braces {index}
  • Keyword parameters - list of parameters of type key=value, that can be accessed with key of parameter inside curly braces {key}

The above code will print the following output:

We are the knights who say "Nothing!"
We are the knights who say "Nothing!"
We are the Nothing who say "knights!"
This spam is absolutely horrible.
This absolutely horrible is spam.
The story of Bill, Manfred, and Georg.


sys.stdout.write output data to console.

There's at least one situation in which you want sys.stdout instead of print.

When you want to overwrite a line without going to the next line, for instance while drawing a progress bar or a status message, you need to loop over something like.

When you execute above code only count will be updated instead of printing message 10 times.