Home » Blog » Count Word, Line, Words Start From Vowel From File Python

Count Word, Line, Words Start From Vowel From File Python

Write a python program that reads the contents of the file poem.txt and count the number of alphabets blank spaces lowercase letters and uppercase letters the number of words starting from vowel and the number of occurrences of each word in the file.

alphabet=0
space=0
lower=0
upper=0
voword=0
oc=0
occur={}

f=open("data.txt","r")
content=f.read()
vowel=['a','e','i','o','u','A','E','I',"O",'U']
print("Contet Of File")
print(content)
print("\nTotal Length Of File",len(content))
words=content.split(' ')
#print(words)

for i in range(len(content)):
    #print(content[i])
    if (content[i]>='a' and content[i]<='z') or (content[i]>='A' and content[i]<='Z'):
        lower+=1
        alphabet+=1
        if  content[i]>='A' and content[i]<='Z':
            upper+=1
    if(content[i]==' '):
        space+=1

for i in range(len(words)):
    str=words[i]
    oc=0
    if str[0] in vowel:
        voword+=1
    for j in range(len(words)):
        if words[i]==words[j]:
            oc+=1
    occur[words[i]]=oc
        
print("Number Of Alphabet ",alphabet)
print("Number Of Space ",space)
print("Number Of Lowercase Letter ",lower)
print("Number Of Uppercase Letter ",upper)
print("Words Starting From Vowel",voword)
print("\nOccurance Of Word\n")
for item in occur:
    print(item," => ",occur[item])
  • OutPut
Contet Of File
Hi this is python lab. Python is high level programming language.

Total Length Of File 65
Number Of Alphabet  53
Number Of Space  10
Number Of Lowercase Letter  53
Number Of Uppercase Letter  2
Words Starting From Vowel 2

Occurance Of Word

Hi  =>  1
this  =>  1
is  =>  2
python  =>  1
lab.  =>  1
Python  =>  1
high  =>  1
level  =>  1
programming  =>  1
language.  =>  1
​
​

 

1 thought on “Count Word, Line, Words Start From Vowel From File Python”

Leave a Reply

Your email address will not be published.