Python Introduction

Python Input Output (I/O) Using input() and print() Function

A good program should effectively communicate any input in Python from the user and display a result to the outside world. A user can give a program input manually from a keyboard or use data from an external source, such as a file or a database. Python offers us several built-in functions that make it easy to create a program quickly. Two of the most commonly used built-in functions are the input() and print() functions that are used for frequent input and output operations, respectively.

Python Output Using print() Function

Once a program accepts input in Python and processes the data, it needs to present the data back to the user as output. You can choose to display the data output to the console (or IDE) directly or show it on a screen through a graphical user interface (GUI). You can even send it back to an external source. 

We use the print() function to display the output data to the console (screen), and we follow the following syntax to do this:

Using the above syntax, we can pass several objects (<obj>) in the print() function by separating them with a comma. 

For example:

                    

num = 65
print ('The value of the number is', num)

Output:

                    

The value of the number is 65

Keyword Arguments in print() 

By default, print() separates each object by a single space, as seen in the example above. However, we can change this by using keyword arguments like “sep=” and “end=”. 

These two keywords give us more control over how we want our output to look. Now, you need to remember that we use these keyword arguments in a specific format and that we pass them to print() at the very end (after the list of objects). The format to use the keywords arguments is:

<keyword>=<value>

The sep= Keyword Argument

We use the sep= keyword argument (sep=<str>) to separate the objects with specific strings (<str>) instead of the default single space. 

Let’s look at an example of how we can use this keyword argument:

                    

print ('Good', 8, 'morning')
 
print ('Good', 8, 'morning', sep = '/')
 
print ('Good', 8, 'morning', sep = '...')
 
print ('Good', 8, 'morning', sep = ' --> ')

The output for each print() function will be:

                    

Good 8 morning
Good/8/morning
Good...8...morning
Good --> 8 --> morning

On the other hand, if you don’t want any space between each object, all you have to do is leave the <str> argument empty, like this:

                    

print ('Good', 8, 'morning', sep = ' ')

Output:

The end= Keyword Argument

Apart from a single space, print() also automatically adds a new line at the end of each output. Using the end= keyword argument, we can cause the output to be terminated by <str> instead of the default newline. 

For example, if you want to show the values in a loop, you can use the end= keyword argument to display them all in one line.

                    

for n in range(15):

print(n)

Output:

                    

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

                    

for n in range(15):

print(n, end=(' ' if n < 7 else '\n'))

Output:

                    

0 1 2 3 4 5 6 7
8
9
10
11
12
13
14

As you can see, we used the end= keyword argument in the above example to display numbers less than 7 in one line, while the numbers after 7 are displayed in a new line.

Output Formatting

Although the above two keyword arguments are useful to format the output string, they only help us change the spacing or decide what will come at the end of the output. However, we can use the str.format() method to get more precise control over the appearance of data.

                    

a = 'mine'; b = 'yours'
print ('This book is {} and that pen is {}' . format(a,b))

Output:

                    

This book is mine and that pen is yours

In the above example, the curly brackets {} act as placeholders using which we can decide the order in which the arguments are printed.

                    

a = 'mine'; b = 'yours'

print ('This book is {} and that pen is {}' . format(b,a))

Output:

                    

This book is yours and that pen is mine

We can also use the tuple index (numbers) to specify the order.

                    

print('He ate {0} and {1}'.format('chocolate','ice cream'))

print('He ate {1} and {0}'.format('chocolate','ice cream'))

Output:

                    

He ate chocolate and ice cream

He ate ice cream and chocolate

Or use keyword arguments for formatting:

                    

>>> print(' {greeting}, {name}'.format(greeting = 'Nice to see you', name = 'Apollo'))
Nice to see you, Apollo

We can also use the % operator to introduce conversion specifiers to format strings:

                    

>>> s = 73.72894

>>> print('The value is %3.2f' %s)

The value is 73.73

Python Input

To run an application, programmers often need to obtain input in Python from a user. The simplest way to do this is to use the input() function. The function pauses program execution to let the user type a line of information from the keyboard. When the user hits “Enter”, the input is read and returned as a string.

We follow the given syntax to use the input() function:

input([<prompt>])

Here, the prompt is an optional string that we wish to display for the user.

                    

>>>name = input ('What is your name? - ')
>>>print ('Hello,', name)
 
What is your name? - John
Hello, John

By default, the input() function accepts only string arguments. However, we can this to a number by using the int() of float() functions

                    

>>>number = input ('What is your number? ')
 
What is your number? 45
>>> number
'45'
>>> int(45)
45
>>> float(45)
45.0

Another function called eval() performs the same operation as int() and float(). However, eval() can analyse expressions as well, provided the input in Python is a string.

                    

>>> int('76 + 5')

Traceback (most recent call last):

  File "", line 1, in 

ValueError: invalid literal for int() with base 10: '76 + 5'

>>> eval('76 + 5')

81

Python Import

Programmers often deal with large programs that make them more difficult to manage. To make programming easier: it is a good idea to divide a program into smaller modules (a file containing Python statements and definitions). These modules are given unique file names and end with the .py extension. 

A great feature of Python is that it lets us import the statements and definitions in modules to other modules or the Python interpreter by using the import keyword.

For example, we can import the math module that allows us to access all the definitions in the module.

                    

>>> import math

>>> print (math.remainder(105,2))

1.0

If you do not want to import all the definitions, you can use the from keyword to only import specific attributes and functions.

>>> from math import e

>>> e

2.718281828459045

Questions and Answers

Q1. What is input () in Python?

Answer: In Python, the input() function is used to accept data from a user or external source and returns this data in the form of a string. The input in Python can come manually from a user (through a keyboard), a file, or even a database.

Q2. How do you write input in python?

To use input() in Python we follow the syntax:

input([prompt])

To accept data from a user. With this function, a programmer can choose to display a prompt message for the users, as well. 

                    

# input without a prompt

takeString = input()

print ('The message is :', takeString)



>>>Hi, how are you?

The message is: Hi, how are you?



# input with a prompt

takeString = input('What would you like to say?')

print ('The message is:', takeString)



>>> What would you like to say?

>>> Hi, how are you?

The message is: Hi, how are you?

Q3. What type is input in python?

By default, the input() function accepts any input type from the user. However, it converts all the input into a string datatype. If you want to display a number, you can use the int() or float() functions to convert the string data.

                    

>>>number = input ('Input a number: ')

Input a number:  

92

>>> number

'92'

>>> int(45)

92

>>> float(45)

92.0

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.