0% found this document useful (0 votes)
10 views8 pages

Text File Handling Questions With Answers

The document provides a series of Python code snippets for reading and analyzing text from a file named 'data.txt'. It covers counting characters, digits, words, lines, vowels, consonants, and specific word patterns, as well as displaying words based on various criteria. Each task is implemented as a separate function with clear instructions and examples.

Uploaded by

mihirpgm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Text File Handling Questions With Answers

The document provides a series of Python code snippets for reading and analyzing text from a file named 'data.txt'. It covers counting characters, digits, words, lines, vowels, consonants, and specific word patterns, as well as displaying words based on various criteria. Each task is implemented as a separate function with clear instructions and examples.

Uploaded by

mihirpgm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

WAP to read and count character from data.

txt file :

i. Character Upper/ Lower Character

ii. Digit Character Characters/Symbol/ Spaces

iii. Words/ Lines

iv. Vowel /Consonant Character

ANS:

i/ii:

f1=open("data.txt","r")

s=f1.read()

print(s)

count=0

for ch in s:

if ch.isupper()==True: #ch.islower()/ch.isdigit()/ch.isspace/ ch.isdigit()/ch==’\n\ for line

count+=1

print(count)

words=count+1 # After Space Count

print(words)

Symbol (Special Characters):


f1=open("data.txt","r")

s=f1.read()

count=0

for ch in s:

if ch.isalnum()==False:

count+=1

print(count)

1 (iii:) count Words from data.txt file :


f1=open("data.txt","r")

s=f1.read()
print(s)

words=s.split()

print(words,”, ”,len(words))

#count Line from data.txt file :

f1=open("data.txt","r")

s=f1.readlines()

print(s)

print(s,”, ”,len(s))

1(iv:) Count Vowel /Consonant Character

f1=open("data.txt","r")

s=f1.read()

countV=0

countC=0

for ch in s:

if ch.isalnum()==True:

if ch=='a' or ch=='e' or ch=='i' or ch=='o' or

ch=='u':

countV+=1

else:

countC+=1

print("V: ",countV,", C= ", countC)

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))

for word in words:

if word[0].isupper()==True: #same Q1 part 1st

count+=1

print(count)

3. WAF to read and count Words 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))

for word in words:

if word[-1].isupper()==True: #same Q1 part 1st

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))

for word in words:

if word[-1].isupper()==True and word[0].isupper()==True: #same Q1 part 1st

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)

sword=input(“Enter Your search Word: ”)

words=s.split()

print(words,”, ”,len(words))

for word in 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

sch=input(“Enter Any Character: ”)

for ch in s:

if ch==sch[0]: #sch[0] read first character from input string

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))

for word in words:

if word[0].isupper()== ‘A’: # word[-1] for end character/ endswith or startswith method

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))

for word in 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))

for word in 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))

for word in 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))

for word in words:

countV=0
countC=0

for ch in word:

if ch.isalnum()==True:

if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':

countV+=1

else:

countC+=1

print("Word : ",word,", V: ",countV,", C= ", countC)

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=""

for word in words:

countV=0

for ch in word:

if ch.isalnum()==True:

if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':

countV+=1

if maxV<countV:

maxV=countV

final=word

print("Final : ",final,", maxV: ",maxV)


Q13. WAF to read data from a text file DATA.TXT, and display word which have
maximum/minimum characters.

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=""

for word in words[1:]:

length=len(word)

if maxC<length:

maxC=length

maxfinal=word

if minC>length:

minC=length

minfinal=word

print("Max word : ",maxfinal,", maxC: ",maxC)

print("Min word : ",minfinal,", minC: ",minC)

You might also like