Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

Home » Python

Input and Output Operations with Examples in Python

Learn: How to perform input and output operations in python using suitable methods/functions with Examples in Python?
Submitted by Abhishek Jain, on August 10, 2017

A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available for Input.

1) input()


input (expression)

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.


>>>x= input ("Enter data:")
Enter data:  34.78


2) raw_input()


raw_input (expression)

This input method fairly works in older versions (like 2.x).

If prompt 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 enter key.

We can use raw_input() to enter numeric data also. In that case we typecast, i.e., change the data type using function, the string data accepted from user to appropriate Numeric type.


>>>y=int(raw_input("Enter your roll no."))

Enter your roll no. 5

It will convert the accepted string i.e., 5 to integer before assigning it to 'y'.

Print statement


print (expression/constant/variable)

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)

Sponsored Links