Input output in python

A portion of the functions like input() and print() are broadly utilized for standard input and output tasks individually in Python.

Python Input

In Python, we have the input() function which accepts the data from the user as input during runtime.

The input( ) takes whatever is typed from the keyboard and stores the entered data in the given variable. If prompt string is not given in input( ) no message is displayed on the screen, thus, the user will not know what is to be typed as input. In Python v2 we use raw_input() instead of input(), and the syntax for raw_input() is: name = raw_data(‘Enter name’).

The structure for input() is:

input([text])

where text is the string which will be displayed when the user, to know what input can be given.

age = input("enter your age")

Here, the user needs to give an input which says as to enter your age.

enter your age 50

Here, we can see that the entered age 50 is a string, not a number. To change over this into a number we can use int() or float() functions.

print("your age is",int('50'))

Now, your age variable is in integer.

your age is 50

Let’s consider another example:

num1 = int (input(“Enter Number 1: ”))
num2 = int (input(“Enter Number 2: ”))
print ("The difference = ", num1 - num2)

Output:

Enter Number 1: 70
Enter Number 2: 20
The difference =  50

Python Output

We use the print() function to display result on the screen.

The print ( ) evaluates the expression before printing it on the monitor. The print () displays an entire statement which is specified within print ( ). Comma ( , ) is used as a separator in print ( ) to print more than one item.

An example of use print() is given below.

print("Python is an object oriented language")

Output

Python is an object oriented language

The actual syntax of print() 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 another line.
  • The file is where the qualities are printed and its default value is sys.stdout (screen).

Here is a guide to show this.

print('a', 'b', 'c', 'd')
print('a', 'b', 'c', 'd', sep='+')
print('a', 'b', 'c', 'd', sep='+', end='$')

Output

a b c d
a+b+c+d
a+b+c+d$

Now and then we might want to design our output to make it look alluring. This could be possible by utilizing the str.format() method. This method is visible to any string object.

print("Python is {0} and {1} language".format('object oriented','easy to use'))

Output

Python is object oriented and easy to use language