Input, Output(I/O) And Import In Python

Input, Output(I/O) And Import In Python
Input, Output(I/O) And Import In Python


Python programming language has become synonymous with ‘all-rounder’ and ‘hot potato’. The popularity that Python enjoys among the developers is truly unmatchable for any other programming language.

In python, you will get a number of inbuilt functions which are used to perform specific tasks.

In this article, we’ll look at how to use Python‘s built-in methods input() and print() to execute I/O tasks. You’ll also learn how to import and use modules in your program.

Note: The “Python Input-Output (I/O) & Import” tutorial is now complete. But not your learning. The next lesson you should learn is Python Data types for Beginners.

Before we begin, let us define built-in functions.

A built-in function is already defined in a programming framework with a set of statements that perform a task. As a result, users do not need to create this function and can use it directly in their program or application.

Programming frameworks offer various built-in functions that vary from one to another; each has its own set of capabilities.

Are you interested in learning about all of Python’s built-in functions? The official documentation can be found here.

Without further ado, let’s get started.

Taking Input from the user

We will need a variable to capture the input in your code. A variable is a data container. 

blog banner 1
For example: name = “CodingNinjas”
So here, name is a variable that holds a value “CodingNinjas”.

In the above code, the value of the variable was already defined. We might want to take user input to provide more flexibility. In Python, we have the input() function that allows us to do this. 

The syntax for input() is:

where ‘prompt’ is the string that we would like to see on screen. It is optional.

You can assign the input to a variable. This is accomplished by placing the variable name before the = operator and then the input keyword.

Below are a few examples of taking input from users depending on use cases.

Example 1: Without Prompt

Name = input() # The data you enter will be assigned to a Name variable.

Example 2: With Prompt

Name = input(“What is your name?”) #Here, the prompt is “What is your Name?”.

Example 3: Typecast string input to another data type

Python stores all variables as strings; to explicitly typecast them, we can use int(), float(), and many other functions along with the input() method. Let’s see how this works.

num = int(input(“Enter a number: “)) #Changes string input to int

So far, we have discussed how to input from the user in Python. Now, let’s proceed to see how to print an output onto the screen?

Output using the print() function

The print() function in python is used to display the output on the screen/console.

Simply call the print function and pass the parameters. Note: Parameters are the values you write within the opening and closing bracket.

For example: find(a, b), here find is a function with ‘a’ and ‘b’ as parameters. The actual syntax of the print() function is:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
  • objects: The value(s) to be printed are called objects.
  • sep: separator is used between the values. It defaults into a space character.
  • end: It is used after all values are printed. A new line is created by default.
  • flush: whether to forcibly flush the stream.

Remember, the values of sep and end can be modified. For example, it can be modified to a comma, hash, special symbol, number, etc.

Below are some examples of how print() can be used.

Example 1: Without Parameters

print(“Welcome to Coding Ninjas”)


Welcome to Coding Ninjas

Example 2: With Parameters

print("Addition of",num1,"and",num2,"equals to",sum)

The output of the above code after passing num1,num2 and sum as parameters will be:

Addition of 5 and 10 equals to 15

Using print() in more detail

  • Let’s say you wish to print a particular string (a collection of characters such as letters, punctuation marks, digits, and letters) N times. On strings, the (asterisk) * operator performs repetition. Put “A” followed by * and the number of times you want “A” to be repeated inside the print parenthesis.


  • We may want to format the output to make it more appealing. The str.format() method can be used to accomplish this.

x = 5
y = 10
string = 'The value of x is {} and y is {}'

print(string.format(x, y)) #or you could simply write
print('The value of x is {} and y is {}'.format(x, y))

The value of x is 5 and y is 10
The value of x is 5 and y is 10

  • Curly braces {} are utilised as placeholders in str.format(). We can use numbers(tuple index) to specify the order in which they are printed.
print('I am good in {0} and {1}'.format('Maths','Coding'))
print('I am good in {1} and {0}'.format('Maths','Coding'))

I am good in Maths and Coding
I am good in Coding and Maths

Moving on from Python inputs and outputs, how do we handle the data that the user enters as input?

We require a particular level of functionality from the code. We have two options for this as well:

  • Create the functionality from scratch.
  • Use existing libraries to continue our program.

It can be simple to create functions from scratch, but it can also be time-consuming and frustrating. Because Python is so widely used, the simplest and most favored method is to use libraries. Hundreds of thousands of libraries are available to use in Python.

So, let’s work with user input and add certain functionalities.

Python Import

It is a good idea to separate our code into different modules as it gets bigger.

And In Python, It can be done using Modules.

  • A module is a Python file that contains definitions and statements. 
  • Modules in Python have a filename and extension.
  • A module’s definitions can be imported into another module or Python’s interactive interpreter. To do so, we use the import keyword.

Assume the user enters a number, and we want our code to return the number’s square root. 

The function sqrt() is found in the math module. To utilise this function, we must first import the module. So, let’s have a look at how importing the module will give our code a boost.

#first import math module
import math
#get a variable having value 25
#square root this number.

You will get the square root of 25 (which is 5) if you run this code.


We can also access specific attributes from the module using the from keyword.  “Import this function, method, or attribute from this module,” it says.

Consider the following case:

from math import pi

Let’s move on to some frequently asked questions on Python Input Output (I/O) & Import functions.

Frequently Asked Questions

How do you input and output in Python?

In Python, the input and output are done using built-in functions input() and print().

How do you get output in Python?

In Python, print() is used to display output on the screen/console.

What is input() in Python?

In Python, input() is a built-in function that helps in taking input from the user.

What is the default data type of input statement in Python 3?

A string is the default data type of input statement in Python 3.

What does raw input mean in Python?

The raw input() method in Python is used to read a string from a standard input device such as a keyboard.

How do you import something into Python?

We can import modules using the import keyword.

Is a Python file a module?

Any Python file can be referenced as a module. A file containing Python code, for example,, is called a module.

Key Takeaways

Superb! Indeed you learned everything you need to know about accepting user input in Python and printing the needed outputs. We also learned how to use Python’s import command to extend the functionality of our code and interact with inputs.

If you aspire to be a Python programmer. Don’t forget to check out the free course on Python Foundation with Data Structures & Algorithms.

The course will train with the core programming concepts with Python. You will become eligible to apply for positions at Google, Microsoft, Facebook, and more. 

You can click on the link and check out other Free courses on C++, Java, Machine Learning etc., offered by Coding Ninjas!

By Aanchal Tiwari