Text File Handling Questions With Answers
Text File Handling Questions With Answers
txt file :
ANS:
i/ii:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
for ch in s:
count+=1
print(count)
print(words)
s=f1.read()
count=0
for ch in s:
if ch.isalnum()==False:
count+=1
print(count)
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
f1=open("data.txt","r")
s=f1.readlines()
print(s)
print(s,”, ”,len(s))
f1=open("data.txt","r")
s=f1.read()
countV=0
countC=0
for ch in s:
if ch.isalnum()==True:
ch=='u':
countV+=1
else:
countC+=1
2. WAF to read and count Words start with from text file : Upper /Lower / Digits / Special / Vowel
/Consonant Character
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
count+=1
print(count)
3. WAF to read and count Words end with from text file : Upper /Lower / Digits / Special / Vowel /
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
count+=1
print(count)
4. WAF to read and count Words start and end with from text file : Upper /Lower / Digits / Special
/ Vowel /Consonant/User Define Character
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
count+=1
print(count)
5.WAF to count the word present in a text file DATA.TXT. Word: - like this/This, My, Me, He, She,
to, the, do, Mr. and Mrs.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
if word==sword:
count+=1
print(count)
Q6. WAF to count the number of character present data.txt as : ‘A’, ‘t’, ‘T’, ‘E’, ‘I’, ‘i’.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
for ch in s:
count+=1
print(count)
Q7. WAF to count the number of words/lines in a text file ‘DATA.TXT’ which is started/ended with
‘A’, ‘t’, ‘T’, ‘E’, ‘I’,‘i’.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words,”, ”,len(words))
count+=1
print(count)
Q8. WAF to count the number of words/lines in a text file ‘DATA.TXT’ which is starting/ended with
an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words,”, ”,len(words))
if word.startswith(“The”)==True: # word.endswith(“The”)
count+=1
print(count)
Q9. WAF to read data from a text file DATA.TXT, and display those words, which are less than 4
characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words,”, ”,len(words))
if len(word)==4:
print(word)
count+=1
print(count)
Q10. WAF to read data from a text file DATA.TXT, and display words with number of characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,”, ”,len(words))
print(word,”, ”,len(word))
Q11. WAF to read data from a text file DATA.TXT, and display each words with number of vowels
and consonants.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,”, ”,len(words))
countV=0
countC=0
for ch in word:
if ch.isalnum()==True:
countV+=1
else:
countC+=1
Q12. WAF to read data from a text file DATA.TXT, and display word which have maximum number
of vowels characters.
ANS:
f1=open("data.txt","r")
s=f1.read()
print(s)
countV=0
countC=0
words=s.split()
print(words,", ",len(words))
maxV=0
final=""
countV=0
for ch in word:
if ch.isalnum()==True:
countV+=1
if maxV<countV:
maxV=countV
final=word
Ans:
f1=open("data.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
length=len(word)
if maxC<length:
maxC=length
maxfinal=word
if minC>length:
minC=length
minfinal=word