You are on page 1of 6

Courses

Free Courses Interview Questions Tutorials Community

Home /
Tutorial /
Python Input and Output

Python Input and Output


By Manoj 9.7 K Views 14  min read Updated on December 28, 2020

While working in a programming language, you may require to input some data and then use instruction to output a value. Also,
you might work on files that may need to be opened, closed, read, or write. Thus, you should have knowledge of input and
output in Python. In this part of the Python tutorial, you learn how to input and output data. Also, you will gain knowledge about
file handling.

Become a Certified Professional

Python Tutorial

Install Python?

Python Syntax

Python Input and


Output

Comments in Python

Data Types in Python

Python Variables -
Constant, Global & Static
Variables

Numbers in Python

String in Python

Python Lists

Tuple in Python

Python Sets

Python Dictionary

Python Operators

Type conversion in
Python

Python If Else
Statements

Python While Loop

For Loop in Python

Python Functions -
y
Define & Call a Functions
in Python

Lambda Function in
Python

Python Built in
Functions with Examples

Python Arrays

Python Classes and


Objects

Python Modules

Python Dates

Python JSON

Python RegEx

PIP Python

User Input

The input Function


The input() function is used to get data from the user in Python Command Line programs.

Syntax

<return> = input( "<prompt>")

Where prompt is a string that will be display for the user to view which should provide some kind of indication of the usage
and type of data you are expecting and return is the returned value from the function.

e.g.

>>> i = "Hello Intellipaat"

>>> x = input("enter a value:")

enter a value: i

>>> print i

Output

Hello Intellipaat

raw_input() function
raw_input() function is used to take input from the user. It takes the input from the Standard input in the form of a string
and reads the data from a line at once.

Syntax

raw_input(?statement?)

e.g.

i=raw_input("Enter value ");

print i

Output

Enter value

Hello intellipaat

raw_input() does not deal with numbers, at least not directly.

e.g.

x = raw_input("Enter your marks: ")

Enter your marks: 20

>>> print x

Then it shows error because numbers are not handled by raw_input() directly. To use numbers conversion is used.

e.g.

x = int ( raw_input("Enter your marks: ") )

Enter your marks: 20

print x

Then it displays 20.

User Output
Print statement is used to get the output.

Syntax

print expression

e.g.

x=20

print x

Output

20

File Handling
File is an external storage on hard disk from where data can be stored and retrieved. Python supports reading data form
files and writing data to the files.

Operations on Files:

Opening a file

– To open file built in function open() is used. It returns an object of File which is used with other functions.

Syntax:

obj=open(file_name , access_mode , buffer)

file_name specify that file which you want to open.

access_mode specify the access_mode in which the file has to be open, i.e., read, write, append, etc.

buffer – It represents that buffering is performed or not, if buffer value is 0 then no buffering is performed and when
buffer value is 1 then line buffering is performed while accessing the file.

Access Modes

Modes Description

r Opens a file for reading

rb Opens a file for reading only in binary format.

w Opens a file for writing only. Overwrites the file if the file exists.

wb Opens a file for writing only in binary format.

a Opens a file for appending. It does not overwrite the file just add the data in the file and if file is not
created then it creates new file

ab Opens a file for appending in binary format.

Closing a File:
It is used to close a file. For this purpose close() function is used.

Syntax:

File_object.close()

Writing to a File:
write() method is used to write a string into the file.

Syntax:

File_object.write(string str)

i=open("intellipaat.txt","w")

i.write("Hello Intellipaat")

i.close()

Reading from a File:


read() method is used to read data from the File.

Syntax:

File_object.read(data)

e.g.

j=open("intellipaat.txt","r")

k=j.read()

print k

j.close()

Output 

Hello Intellipaat

 
Methods in File Handling 
There are different methods are used which are as follows:

rename() : This is used to rename a file.

Syntax:

os.rename(existing file_name, new file_name)

eg:

import os

os.rename("abc.txt","xyz.txt")

remove(): This method is used to delete a file.

Syntax:

os.remove(file_name)

e.g.

import os

os.remove("abc.txt")

chdir(): This method is used to change the current directory.

Syntax:

os.chdir("new directory")

e.g.   

import os

os.chdir("new directory path")

mkdir() : This method is used to create a directory.

Syntax:

os.mkdir("new directory")

e.g.

import os

os.mkdir("abc")

rmdir() : This method is used to remove the directory.

Syntax

os.rmdir("directory name")

e.g.
import os

os.rmdir("abc")  // where abc is a directory name which you want to remove

getcwd()– This method is used to show the current working directory.

Syntax

os.getcwd()

e.g.

import os

print os.getcwd()

This blog will help you get a better understanding of Automate Your Coding with Python!


Previous Next

Course Schedule

Name Date

2022-05-07 2022-05-08

Python Course View Details


(Sat-Sun) Weekend batch

2022-05-14 2022-05-15

Python Course View Details


(Sat-Sun) Weekend batch

2022-05-21 2022-05-22

Python Course View Details


(Sat-Sun) Weekend batch

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

Name * Email *

Post Comment

Browse Categories

You might also like