LIVE Course for free

Rated by 1 million+ students
Get app now
JEE MAIN 2023
JEE MAIN 2023 TEST SERIES
NEET 2023 TEST SERIES
NEET 2023
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
95.6k views
in Computer by (71.0k points)

Write a user defined function in Python that displays the number of lines starting with ‘H’ in the file Para.txt.Eg: if the file contains: 

Whose woods these are I think I know.

His house is in the village though;

He will not see me stopping here

To watch his woods fill up with snow.

Then the line count should be 2.

1 Answer

+1 vote
by (75.9k points)
selected by
 
Best answer

def countH():

f = open ("Para.txt", "r")

lines =0

l =f.readlines()

for i in l:

ifi[0]=='H':

lines+=1

print "no. of lines is",lines

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...