Input / Output in python

 

Python Input / Output


Hello Learners… in this session, we can learn about two built-in functions print() and input() to perform I/O task in Python.


Let us see the output section first.

Python Output using print() function

We use the print() function to output data to the standard output device (screen).

An example of its use is given below.

1
print('Hearty welcome to website')

Output:

Hearty welcome to website

Another example is given below:

1
2
a = 10
print('The value of a is', a)

Output:

The value of a is 10

In the second print() statement, we can notice that space was added between the string and the value of variable a. This is by default, but we can change it.


The actual syntax of the print() function is:
1
print(*objects, sep=' ',end='\n',file=sys.stdout,
flush=False)

objects : the value(s) to be printed.

sep : separator is used between the values. It defaults into a space character.

end : it print after all values are printed. It defaults into a new line.

file : it is the object where the values are printed and its default value is sys.stdout (screen).

Here is an example to illustrate this.

1
2
3
print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end='&')

Output

1 2 3 4
1*2*3*4
1#2#3#4&

Output formatting

Sometimes we would like to format our output to make it look attractive.

This can be done by using the str.format() method.

This method is visible to any string object.

1
2
x = 5; y = 10
print('The value of x is {} and y is {}'.format(x,y))

Output:

The value of x is 5 and y is 10

Here, the curly braces {} are used as placeholders.

We can specify the order in which they are printed by using numbers (tuple index).

1
2
print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))

Output

I love bread and butter
I love butter and bread

We can even use keyword arguments to format the string.

1
print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'Arjun'))

Output:

Hello Arjun, Goodmorning

We can also format strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this.

1
2
3
4
x = 12.3456789
print('The value of x is %3.2f' %x)
 
print('The value of x is %3.4f' %x)

Output:

The value of x is 12.35
The value of x is 12.3457
Python Input using input() function

Up until now, our programs were static.

The value of variables was defined or hard coded into the source code.

To allow flexibility, we might want to take the input from the user.

In Python, we have the input() function to allow this.

The syntax for input() is:

input([prompt])

where prompt is the string we wish to display on the screen. It is optional.

1
2
num = input('Enter a number: ')
print(num)

Output:

10

Here, we can see that the entered value 10 is a string, not a number.

To convert this into a number we can use int() or float() functions.

1
2
print(int('10'))
print(float('10'))

Output

10
10.0

This same operation can be performed using the eval() function. But eval takes it further. It can evaluate even expressions, provided the input is a string

1
2
3
4
5
6
7
a=int('2+3')
print(a)
 
print('---')
 
b=eval('2+3')
print(b)

Output:

line 1, in <module>
a=int('2+3')

ValueError: invalid literal for int() with base 10: '2+3'
---
5

No comments:

Post a Comment