Input and Output in Python

In this tutorial I will discuss about how to take input and show output in python.

In a program taking an input from the user ensures that the program is user friendly as user is able to use the program as per their requirements. And giving the output in proper format helps user to understand the output and use of the program.

Python Input

For a program to be more flexible and user-friendly we need to take input from the users and be able to manipulate them. To take input from user we have input() function in python.

Following is the basic syntax of input() function.

input([prompt])

where, prompt is the message we wish to display while taking the input. It is optional.

value=input("Enter a value ")
Enter a value 46
print(value, type(value))
46 

As we can see, the entered value is 46 but is a string and not a number. So, we need to convert this into number for which we can use int() or float() functions.

These functions are used for data-type conversion, we will learn them later in-depth.

val1=int(value)
print(val1, type(val1))
46 
val2=float(value)
print(val2, type(val2))
46.0 

Output in python

In python language to print() function is used to show the output onto a standard output device( screen of computer). The print() function can also be used to send output to a file which we look into later.

Let's see some example on how print() function is used.

print("Hello, this sentence is output on the screen ")  #simple print statement

value = 22
print("The value is",value)  #this print statememnt prints an argument
Hello, this sentence is output on the screen 
The value is 22

The basic syntax of a print() statement is as shown :

print(*objects,sep='',end='\n', file=sys.stdout, flush=False)

Here, objects are the values to be printed.

The sep is separator between the values(objects) which is space(' ') by default.

The end value is printed after all the values(objects) are printed, it is basically the end of a print statement.

It has default value of newline('/n').The file is the object where print statement executes and prints the values(objects). It has default value sys.stdout(system screen).

Let's see some examples

print('value1', 'value2', 'value3') 
print('value1', 'value2', 'value3', sep="@$")
print('value1', 'value2', 'value3', sep='$', end='#')
value1 value2 value3
value1@$value2@$value3
value1$value2$value3#

Formatting output

In python, we can format the output as per our need to make it look more attractive. For this we can use str.format() method.

This method is visible for any string object.

x,y = 5, 10
print("The value of x is {} and y is {}".format(x,y))
The value of x is 5 and y is 10

The curly braces ( { } ) are called placeholders which specify the position of the values to be formatted. We can also give the order in which values can be printed in an order using numbers.

a,b = 8,25
print("{0} is less than {1}".format(a,b))
print("{1} is greater than {0}".format(a,b))
8 is less than 25
25 is greater than 8

Keyword arguments can also be used instead of numbers to print values in specific format.

print("Hello {name}, {greeting}".format(name="Joy", greeting="Goodevening"))
Hello Joy, Goodevening

In today's tutorial we have discussed,how to take input from users in python, showing output to users and formatting output to make it more understandable.

If you have any questions please comment below also share your views and suggestions in the comment box.

Pooja is a programmer by profession and love to write articles on Python. She is highly interested in Python, Data Science and Machine Learning. Pooja holds B.E. Computer Engineering degree. She loves teaching and learning new thing.

Leave a Comment