Text File Practice Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

OMSAKTHI

G B PUBLIC SCHOOL
CLASS XII COMPUTER SCIENCE
TEXT FILE PRACTICE QUESTIONS
Write a python function IS_CNT() to read from an existing text file “Story.txt” and
return the line having the maximum number of occurances of the word ‘is’
(eg) if the file contents are:
Today is a sunny day but it is cold outside.
I hope it is like this everyday.
Is it true sunlight is a source of Vitamin D which is needed for our bones?
Output should be:
Is it true sunlight is a source of Vitamin D which is needed for our bones?
def IS_CNT():
d={}
f=open('STORY.txt','r')
s=f.readlines()
for i in s:
d[i]=i.count('s')
print(d)
print(max(d, key=d.get))
IS_CNT()

Write a python function REV_TEXT() which read from an existing text file
“Content.txt” and creates another file “New.txt” by copying all lines of Content.txt
with words starting with ‘I’ reversed
(eg) if Content.txt has :
The prisoners were taken to an isolated island
Nobody could identify them
New.txt should have:
The prisoners were taken to an detalosi dnalsi
Nobody could yfitnedi them
def REV_TEXT():
f=open('Content.txt','r')
f1=open('New.txt','w')
s=f.readlines()
for i in s:
word=i.split()
for j in word:
if j[0]=='i':
i=i.replace(j,j[::-1])
f1.write(i)
f.close()
f1.close()

Write a function countmy( ) in Python to read the text file “DATA.TXT” and swap the
first and last letter of each word in the file and display same.
Example: If the file “DATA.TXT” contains:
“This is a website. I have displayed my preferences”
The countmy( ) function should display the output as:
“shiT si a eebsitw. I eavh displayed ym sreferencep ”

def countmy():
f=open('DATA.txt','r')
s=f.readlines()
for i in s:
word=i.split()
for j in word:
i=i.replace(j,j[-1]+j[1:-1]+j[0])
print(i)

f.close()
countmy()

Write a method COUNTLINES( ) in Python to read lines from text file “TESTFILE.TXT”
and display the lines which are not starting with any vowel by swapping the first letter
with the first vowel in that line.
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES( ) function should display the output as:
eW all pray for everyone’s safety.

Write a function Count_words() in Python that counts the number of words containing
digits in it present in a text file “myfile.txt”.
Example: If the “myfile.txt” contents are as follows:
This is my 1st class on Computer Science. There are 100 years in a Century. Student1 is
present in the front of the line.
The output of the function should be: 3

Write a function Count_CAR() to count and display number of lines starting and ending
with same letter in “car.txt”. Also, calculate the average word size.
[Note: Each line end with full stop. Each word is separated by a single space or full stop]
Example:
File content: Ratha madam has a racecar.
She loves to drive the car.
Output should be:
Number of lines= 1
Word average = 3.8
Note: Calculating word average as follows:
Line1: 5+5+3+1+7=21
Line2: 3+5+2+5+3+3=21 = (42/11)=3.8

Write a function RevText() to read a text file “ Input.txt “ and Print only word starting
with “I‟ in reverse order .
Example: If value in text file is: PROUD OF IT
Output will be: PROUD OF TI

Write a function COUNTWORD( ) which counts all the words from the
text file WELCOME.txt whose length is more than 7 or those which
ends with ‘s’ or ‘S’.
For example, if the Book.txt file contains
India is my country
All Indians are my brothers and sister’s
I respect all brothers and sisters
then the output should be: 3

Write a method NO_VOWELS_WORDS() in Python to read the content


of a text file and count how many words contains vowels characters in
the file.
Example: If the file content is as follows:
My first book was
Me and My Family.
It gave me chance to be
Known to the world.
The NO_VOWELS_WORDS() function should display the output as: No.
of such words: 2

You might also like