This tutorial focuses on two built-in functions print() and input() to perform the Input Output and Import task in Python. Also, you will learn to use them in your program.

Python provides numerous built-in functions that are readily available to us at the Python prompt. Some of the functions like input() and print() are widely used for standard input and output operations respectively. Let us see the output section first.

Python Output Using print() function

We use the print() function to output data to the standard output device (screen). We can also output data to a file, but this will be discussed later. An example of its use is given below.

Output : This sentence is output to the screen

Another example is given below:

Output : The value of a is 5

In the second print() the statement, we can notice that space was added between the string and the value of variable a. This is by default, but we can change it. The actual syntax of the print() function is: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False).

Here, objects is the value(s) to be printed. The sep separator is used between the values. It defaults into a space character. After all values are printed, end is printed. It defaults into a new line. The file is the object where the values are printed and its default value is sys.stdout (screen). Here is an example to illustrate this.


Output formatting

Sometimes we would like to format our output to make it look attractive. This can be done by using the str.format() method. This method is visible to any string object.

Here, the curly braces {} are used as placeholders. We can specify the order in which they are printed by using numbers (tuple index).


We can even use keyword arguments to format the string.

We can also format strings like the old sprintf() style used in the C programming language. We use the % operator to accomplish this.

Python Input

Up until now, our programs were static. The value of variables was defined or hard coded into the source code. To allow flexibility, we might want to take the input from the user. In Python, we have the input() function to allow this. The syntax for input() is:

where prompt is the string we wish to display on the screen. It is optional.

Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions.

This same operation can be performed using the eval() function. But eval takes it further. It can evaluate even expressions, provided the input is a string

Python Import

When our program grows bigger, it is a good idea to break it into different modules. A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py. Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this. For example, we can import the math module by typing the following line:

We can use the module in the following ways:

Output : 3.141592653589793

Now all the definitions inside math module are available in our scope. We can also import some specific attributes and functions only, using the from keyword. For example:

While importing a module, Python looks at several places defined in sys.path. It is a list of directory locations.

We can also add our own location to this list.

Python Input Output and Import

The article was published on November 9, 2020 @ 4:08 PM

Leave a Comment