0% found this document useful (0 votes)
287 views3 pages

Class 12 Python Text File Questions

Text file

Uploaded by

crisaswin28
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
287 views3 pages

Class 12 Python Text File Questions

Text file

Uploaded by

crisaswin28
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Text File Practice Questions

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 “[Link]” 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('[Link]','r')
s=[Link]()
for i in s:
d[i]=[Link]('s')
print(d)
print(max(d, key=[Link]))
IS_CNT()

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

Write a function countmy( ) in Python to read the text file “[Link]” and swap the
first and last letter of each word in the file and display same.
Example: If the file “[Link]” 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('[Link]','r')
s=[Link]()
for i in s:
word=[Link]()
for j in word:
i=[Link](j,j[-1]+j[1:-1]+j[0])
print(i)

[Link]()
countmy()

Write a method COUNTLINES( ) in Python to read lines from text file “[Link]”
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 “[Link]”.
Example: If the “[Link]” 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 “[Link]”. 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 “ [Link] “ 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 [Link] whose length is more than 7 or those which
ends with ‘s’ or ‘S’.
For example, if the [Link] 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

Common questions

Powered by AI

To implement this function, read the file line by line, ensure each line is evaluated for the condition that the first and last letter are identical (ignoring punctuation), and if true, count the line. Simultaneously calculate each word's length and keep a running total to determine the average. Use string methods to remove any periods affecting the first and last characters. The code for this looks like: `def Count_CAR(): # open file, evaluate condition per line, # accumulate word sizes for avg calculation` .

To swap the first and last letters of each word, read the file line by line, split each line into words, and for each word, check if it has more than one character to perform the swap. Once the swap is performed, reconstruct the line and print it. The key consideration is maintaining word boundaries and not affecting punctuation. Implement this by replacing each word after transforming it: `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()` .

Read the text file line by line, split it into words, and check each word for the presence of vowels using a regular expression or set membership method. Count each word that contains one or more vowels. This requires handling various cases and ensuring efficiency in search operations. An implementation example could be like: `def NO_VOWELS_WORDS(): # open file, loop through words and check for vowels using set or regex` .

To achieve this, read each line, check if it starts with a consonant by excluding vowels, identify the first vowel in the line, then swap it with the first character. It requires iterating over the characters of the line and conducting swaps after establishing conditions for consonants and vowels. The idea is demonstrated with: `def COUNTLINES(): # implementation details to check and swap # not provided but involves iterating and swapping as needed` .

Read the file and split its content into individual words, then use a conditional check to count how many words contain digits. Care needs to be taken to account for varying positions and multiple digits within words. Challenges include handling punctuation and ensuring words like '1st' are distinctly counted as containing digits. An example methodology: `def Count_words(): # details of reading and checking digits # focus on looping over words in lines` .

To implement this function, you can read the file line by line, count the occurrences of the word 'is' in each line, and keep track of the line with the maximum count. This can be achieved using a dictionary to store the count of 'is' for each line and then finding the maximum value in the dictionary. The code from the document provides a structure like: `def IS_CNT(): d={} f=open('STORY.txt','r') s=f.readlines() for i in s: d[i]=i.count('is') print(max(d, key=d.get)) IS_CNT()`. Ensure that the count method checks for the whole word to avoid partial matching like 'this' .

You need to read the file line by line, then split each line into words. For each word that starts with 'I', reverse the word and replace it in the line. Write the modified lines into a new file. The implementation involves opening the files, iterating through each line and word, reversing the words starting with 'I', and writing the changes to the new file: `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()` .

Read the file and split lines into words, then apply conditions to check if each word has more than seven characters or if it ends with 's'. Use Python's string functions for these checks and increment a counter if the criteria are met. Ensure robustness when handling different cases of 'S'. The function may appear as: `def COUNTWORD(): # read file, iterate over words, apply conditions, # count and report` .

The process involves reading the entire file, splitting it into lines and then words. For every word longer than a single character, swap the first and last characters. Implementing this requires careful handling of word boundaries and ensuring punctuation is not incorrectly altered. Pitfalls include managing edge cases like single-character words or punctuation affecting swaps. The method resembles: `def countmy(): # read input, iteratively swap letters, preserve word and punctuation integrity` .

To reverse each word starting with 'I', read the file and split it by space or punctuation, iterate through the list of words, apply a reverse operation on those starting with 'I', and construct the modified version to print. It's crucial to isolate each word correctly and apply reversal efficiently. A Python code structure like: `def RevText(): # read file and identify words, reverse if starts with I, then print` .

OMSAKTHI 
G B PUBLIC SCHOOL 
CLASS XII COMPUTER SCIENCE 
TEXT FILE PRACTICE QUESTIONS 
Write a python function IS_CNT() to re
The countmy( ) function should display the output as: 
“shiT si a eebsitw. I eavh displayed ym sreferencep ” 
 
def countmy()
Output will be: PROUD OF TI 
 
Write a function COUNTWORD( ) which counts all the words from the 
text file WELCOME.txt whose

You might also like