Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
A. Write a Python function that displays the number of times the word “Python”
appears in a text file named “Prog.txt”. OR
B. Write and call a Python function to read lines from a text file STORIES.TXT and
display those lines which doesnβt start with a vowel (A, E, I, O, U) irrespective of
their case.
Answer:
A.
def count_python():
count = 0
with open(“Prog.txt”, ‘r’) as file:
text = file.read()
words = text.split()
for word in words:
if word.lower() == “python”:
count += 1
print(“The word Python appears”, count, “times.”)
OR
B.
def display_non_vowel_lines():
with open(“STORIES.TXT”, “r”) as file:
print(“Lines that don’t start with a vowel:”)
lines = file.readlines()
for line in lines:
if line[0].lower() not in ‘aeiou’:
print(line)
display_non_vowel_lines()