Advertisements
Advertisements
Question
Write a function count_Dwords() in Python to count the words ending with a digit in a text file "Details.txt".
Example:
If the file content is as follows:
On seat2 V1P1 will sit and
On seat1 VVIP2 will be sitting
The output will be:
The number of words ending with a digit is 4
Code Writing
Advertisements
Solution
def count_words():
f=open("Details.txt","r")
lst=f.readlines()
wrd=""
count=0
for ln in lst:
Line=ln.split(' ')
for wrd in Line:
if wrd[−1]>='0' and wrd[−1]<='9':
count+=1
wrd=""
print("No. of words ending with a digit is:", count)
f.close() shaalaa.com
Reading from a Text File in Python
Is there an error in this question or solution?
