Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

readlines

'''write a program using python functions to count the number of 
lines beginning with “a” in “file.txt” '''

source code:

def counta() :
    f=open('file.txt')
    p=f.readlines()
    count=0
    for i in p:
        if i[0].upper()=='A':
            count+=1
    print(" number of lines initiating with word a",count)
counta()

'''file.txt'''
The life that I have
Is all that I have
And the life that I have
Is yours
The love that I have
Of the life that I have
Is yours and yours and yours.
A sleep I shall have
A rest I shall have
Yet death will be but a pause
For the peace of my years
In the long green grass
Will be yours and yours and yours.

-+-+--+-++-+-+-+-+-+--+-+-++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

output:
number of lines initiating with word a 3





 
PREVIOUS NEXT
Tagged: #readlines
ADD COMMENT
Topic
Name
1+6 =