1 0
Read Time:1 Minute, 4 Second

split()

We have file abc.txt that has 3 lines written in it.

For more Educational Updates on programming
web development , Data Science
Personality development subscribe to TopperG.

Q.1 Display total number of words of a text file abc.txt

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
y = x.read()
z = y.split()
print(len(z))
x.close()

OUTPUT : total words in our files is 16

See also  working with functions important programs part 1

16

Q.2 Display words that start with vowel characters

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d = x.read()
dwl = d.split()          # list of words
for i in dwl:
    if i[0] in 'aeiouAEIOU':
        print(i)
x.close()

OUTPUT : total 3 words starting with vowel word is displayed

Educational
Updates
on

Q.3 Count and display words that start with vowel in the given file

x = open(r"C:\Users\gaurav garg\Desktop\abc.txt",'r')
d = x.read()
dwl = d.split()
c = 0
for i in dwl:
    if i[0] in 'aeiouAEIOU':
        c = c + 1
        print(i)
print("total words that start with vowel : ",c)
x.close()

OUTPUT : total 3 words starting with vowel word is displayed and also count of total number of words starting with vowel is displayed.

See also  File Handling in Python CBSE Class 12
See also  Reading and Writing CSV Files in Python

Educational
Updates
on
total words that start with vowel : 3

Happy
Happy
%
Sad
Sad
%
Excited
Excited
%
Sleepy
Sleepy
%
Angry
Angry
%
Surprise
Surprise
%