A Program needs to interact with end user to accomplish the desired task, this is done
using Input-Output facility. Input means the data entered by the user (end user) of the
In python, we have raw_input() and input ( ) function available for Input.
Syntax of raw_input() is:
x=raw_input (“prompt message”)
If prompt message is present, it is displayed on the monitor after which user can provide the data from keyboard. The function takes exactly what is typed from keyboard, convert it to string and then return it to the variable on LHS of =.
Example (in interactive mode)
>>>x=raw_input ("Enter your name: ")
Enter your name: ABC
x is a variable which will get the string (ABC), typed by user during the execution of
program. Typing of data for the raw_input() function is terminated by pressing enter key.
We can use raw_input() to enter numeric data also. In that case we typecast, i.e., change
the datatype using function, the string data accepted from user to appropriate Numeric
y=int(raw_input(“enter your roll no”))
enter your roll no 5
will convert the accepted string i.e. 5 to integer before assigning it to y.
Syntax for input() is:
input (“prompt message”)
If prompt is present, it is displayed on monitor, after which the user can provide data
from keyboard. Input takes whatever is typed from the keyboard and evaluates it. As
the input provided is evaluated, it expects valid python expression. If the input provided is not correct then either syntax error or exception is raised by python.
Enter data 5
This will set 5 to x
x= input ("Enter data:")
Enter data: 2+1/2.0
Will supply 2.5 to x
i) Exceptions are raised for non-well formed expressions.
ii) Sometimes well formed expression can wreak havoc.
Print evaluates the expression before printing it on the monitor. Print statement outputs
an entire (complete) line and then goes to next line for subsequent output (s). To print
more than one item on a single line, comma (,) may be used.
>>> print “Hello”
>>> print 5.5
>>> print 4+6
>>>print “the value of x=”,x
The value of x=12
We can also use C like format strings
>>>print “x=%.2f” %x