Python I/O and import

This lesson focuses on the two built-in functions, print() and input(), to complete I/O tasks in Python. You will also learn how to use imported modules in your program.

IO in Python

At the Python prompt, we have access to a large number of built-in functions that Python offers. Input() and print() are two often used methods for common input and output operations. Let’s start with the output part.

Python Output using the print() method

We output data to the standard output device using the print() method (screen). We can also output data to a file, but we’ll discuss that later.

Example 1

marks = 75
print('Math Marks =', marks)


Math Marks = 75

You can see that the Python interpreter placed a space between the string “Math Marks =” and the variable value “75” in the print() instruction. Although we can modify it, this is the default.

The print() function’s real syntax is as follows:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
  • In the above syntax, the value or values to print, in this case, are objects.
  • Between the values, the sep separator is utilized. It defaults to the “space.”
  • The “end” is displayed following the printing of all values. It enters a new line by default.
  • The object’s default value for the file, where the values are printed, is sys.stdout (screen). Here is an illustration of that.


print(100, 150, 152, 88)
print(100, 150, 152, 88, sep='*')
print(100, 150, 152, 88, sep='#', end='$')


100 150 152 88

Python output formatting

In Python, the output can occasionally benefit from formatting to make it more appealing using the str.format() method. Any object that is a string can see this method.


mathMarks = 90; engMarks = 100
print('Math marks are {} , and English marks are {}'.format(mathMarks,engMarks))


Math marks are 90, and English marks are 100

The curly braces (), in this instance, serve as placeholders. We can use numbers to specify the order in which they are printed (tuple index).


print('You are best in {0} and {1}'.format('Math','English'))


You are best in Math and English

Additionally, we can format strings using the outdated sprintf() method from the C programming language. To do this, we use the percent operator.


mathMarks = 99.99
print('Math marks are %3.2f' %mathMarks)


Math marks are 99.99

Python input using the input() method

Our programs were static till recently. Variable values were predetermined or hard-coded into the source code. We could wish to take user input to provide flexibility. The input() method in Python allows us to do this. input(syntax )’s is as follows:

Basic Syntax


When a prompt denotes the text on the screen, It’s not required.


mathMarks = input('Enter the math Marks: ')


Enter the math Marks: 99.99
> mathMarks
> 88

We can see that the entered value for the first time, which is a string rather than a number, is 99.99. Again we enter the variable name mathMarks, python interpreter prompts us to enter the number. And the second time, we can enter a different number. Moreover, we can use the int() or float() functions to turn this into a number by entering float(88) and int(88).

Python import() method

Segmenting our program into various modules is a good idea as it gets bigger. A file containing definitions and statements in Python is known as a module, and python modules have a filename and suffix at the end. In Python, you can import a module’s definitions into another module or Python’s interactive interpreter. To do this, we employ the import keyword.

For instance, by entering the line shown below, we can import the math module:

import math



All of the math module definitions are now accessible in our scope. Using the keyword, we can also import only specific characteristics and functions.

For instance

from math import pi
> pi

This concludes the Python I/O and import lesson. In the next lesson, you will learn different control flow statements in Python and their usage.