Python Tutorial | Python Data File Handling 9

Question: 6
Python program to read the contents of the file “abc.txt” word by word and display the contents. and also display total number of words ending with “t” or “T” in the file. and also display the length of each word.
Sol:

def count_words():
     w=0
     with open("abc.txt") as f:
         for line in f:
             for word  in line.split():
                 i=len(word)
                 if(word[i-1]=="T" or word[i-1]=="t"):
                     print(word,"  ",len(word))
                     w=w+1
         print("total words ending with 't' are ",w)
 function calling
 count_words()

Question : 7
Python program to read the contents of the file “abc.txt” word by word and display the contents. and also display total number of words ending with vowels and also display length of each word.
Sol:

def count_words():
     w=0
     vowels='aeiouAEIOU'
     with open("abc.txt") as f:
         for line in f:
             for word  in line.split():
                 i=len(word)
                 if(word[i-1]in vowels):
                     print(word,"  ",len(word))
                     w=w+1
         print("total words ending with vowels are ",w)
 function calling
 count_words()

Question : 8
Python program to read the contents of the file “abc.txt” word by word. Check and print total “the” present in the file.
Sol:

def count_words():
     w=0
     with open("abc1.txt") as f:
         for line in f:
             for word  in line.split():
                 n=word.lower()
                 if(n=="the"):
                     w=w+1
         print("total words 'the' present:  ",w)
 function calling
 count_words()

Question : 9
Python program to read the contents of the file “abc.txt” word by word. Check and print total “this” present in the file.
Sol:

def count_words():
     w=0
     with open("abc1.txt") as f:
         for line in f:
             for word  in line.split():
                 n=word.lower()
                 if(n=="this"):
                     w=w+1
         print("total words 'this' present:  ",w)
 function calling
 count_words() 

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling