Assignment # 7
Assignment # 7
#7
Q1. Write a function definition for DISP3CHAR() in PYTHON to read the content of a text
file KIDINME.TXT, and display all those words, which have three characters in it. For
Example : If the content of the file KIDINME.TXT is as follows :
"When I was a small child, I used to play in the garden with my grandma. Those
days were amazingly fun filled and I remember all the moments of that time."
The function DISP3CHAR() should display the following : was the mom and all the
Also include functions to create the text file and display the entire contents of the
text file
Code:
def createfile():
f = open("Kidinme.txt","w")
while True:
str = input("Enter a line of text:")
if len(str)==0:
break
else:
f.write(str+'\n')
f.close()
def display():
f = open("Kidinme.txt","r")
s = f.read()
print(s)
f.close()
def DISP3CHAR():
f = open("Kidinme.txt","r")
l = f.readlines()
for i in l:
w = i.split()
for j in w:
if len(j)==3:
print(j)
f.close()
createfile()
print()
display()
DISP3CHAR()
Output:
Enter a line of text:When I was a small child, I used to play in the garden with my
grandma. Those days were amazingly fun filled and I remember all the moments of that
time.
Enter a line of text:
When I was a small child, I used to play in the garden with my grandma. Those days
were amazingly fun filled and I remember all the moments of that time.
was
the
fun
and
all
the
>>>
Q2. Write a function EUCount() in PYTHON, which should read each character of a text file
IMP.TXT, should count and display the occurrences of alphabets E and U (including
small cases e and u too). For Example : If the file content is as follows :
" Updated information Is simplified by official websites. "
The EUCount() function should display the output as: E:4 U:1
Also include functions to create the text file and display the entire contents of the
text file
Code:
def createfile():
f = open("Imp.txt","w")
while True:
str = input("Enter a line of text:")
if len(str)==0:
break
else:
f.write(str+'\n')
f.close()
def display():
f = open("Imp.txt","r")
s = f.read()
print(s)
f.close()
createfile()
display()
def EUCount():
f = open("Imp.txt","r")
ct1 = 0
ct2 = 0
d = f.read()
for i in d:
if i == "e" or i == "E":
ct1 +=1
elif i == "u" or i == "U":
ct2 +=1
print("E:",ct1,"\tU:",ct2)
f.close()
EUCount()
Output:
Enter a line of text:hello Manage Upload Utopia cake leap
Enter a line of text:
hello Manage Upload Utopia cake leap
E: 4 U: 2
>>>
Q3. Write a program to create a text file and then using function, perform the following:
i) to read the data from a text file and then count the number of vowels in the text file
ii) to count and display the number of blank spaces, alphabets and digits in a text file
iii) count the number of occurrences of the words ‘me’ or ‘my’ present in the text file
iv) reverse and display each line of text from the text file.
v) to display alternate lines of a text file.
vi) to display all the lines ending with ‘o’ or ‘O’ in a text file
vii) to count all the words starting with ‘T’ or ‘t’ in a text file
Code:
def vowel():
f=open("IMP.TXT","r")
countvowel=0
s=f.read()
for i in s:
if i in "aeiouAEIOU":
countvowel+=1
print("Vowels: ",countvowel)
f.close()
def sad():
f=open("IMP.TXT","r")
spaces=0
alphabets=0
digits=0
s=f.read()
s=s.rstrip()
for i in s:
if i == " ":
spaces+=1
elif i.isalpha():
alphabets+=1
elif i.isdigit():
digits+=1
print("Number spaces are",spaces)
print("Number digits are",digits)
print("Number alphabets are",alphabets)
f.close()
def me_my():
f=open("IMP.TXT","r")
my=0
me=0
s=f.read()
u = s.upper()
word = u.split()
for i in word:
if i == "MY":
my+=1
elif i == "ME":
me+=1
print("MY:",my)
print("ME:",me)
f.close()
def rev():
print("REVERSE:")
f=open("IMP.TXT","r")
while True:
s=f.readline()
for i in s[::-1]:
print(i,end = "")
f.close()
def alternate():
f=open("IMP.TXT","r")
s=f.readlines()
for i in range(0,len(s),2):
print("alternate:",s[i],end=" ")
f.close()
def oO():
f=open("IMP.TXT","r")
s=f.readlines()
for i in s:
i = i.rstrip()
if i[-1] in "oO":
print("O_lines: ",i)
f.close()
def T():
f=open("IMP.TXT","r")
T=0
s=f.read()
word = s.split()
for i in word:
if i[0] in "tT":
T+=1
print("T:",T)
f.close()
f=open("IMP.TXT","w")
while True:
str=input("Enter: ")
if len(str) == 0:
break
else:
f.write(str+"\n")
f.close()
2-to count and display the number of blank spaces, alphabets and digits in a text file
Enter: Kunwarveer Singh Bindra
Enter: Arnaav singh sethi
Enter:
Enter the no.: 2
Number spaces are 4
Number digits are 0
Number alphabets are 37
3-count the number of occurrences of the words ‘me’ or ‘my’ present in the text file
Enter: ME and my tea
Enter:
Enter the no.: 3
MY: 1
ME: 1
4- reverse and display each line of text from the text file
Enter: dchgfdiues
Enter: sdgfdye
Enter: 4
Enter:
Enter the no.: 4
REVERSE:
seuidfghcd
eydfgds
4
6-to display all the lines ending with ‘o’ or ‘O’ in a text file
Enter: ok homo
Enter: sdkhweiud'
Enter: hamie masddiweo
Enter:
Enter the no.: 6
O_lines: ok homo
O_lines: hamie masddiweo
7-to count all the words starting with ‘T’ or ‘t’ in a text file
Enter: Tea tom cksdcis
Enter:
Enter the no.: 7
T: 2
>>>
Q4. Write a program to create a text file “ HAPPY.TXT” and then using a function convert
all uppercase characters to lowercase and all lowercase characters to their uppercase
equivalent in another text file “ EXCITED.TXT”.
Code:
f = open("HAPPY.TXT","w")
while True:
str = input("Enter:")
if len(str) == 0:
break
else:
f.write(str+"\n")
f.close()
def upper():
f = open("HAPPY.TXT","r")
z = open("EXCITED.TXT","w")
nstr=""
s = f.readlines()
for i in s:
if i.isupper():
i=i.lower()
elif i.islower():
i=i.upper()
nstr+=i
z.write(nstr)
z.close()
z = open("EXCITED.TXT","r")
w = z.read()
print(w)
z.close()
f.close()
upper()
Output:
Enter: Radhikaa
Enter: BEHL
Enter: hello
Enter:
Radhikaa
behl
HELLO
>>>
Q5. Assuming that a text file named TEXT1.TXT already contains some text written into it,
write a function named vowelwords(), that reads the file TEXT1.TXT and creates a new file
named TEXT2.TXT, which shall contain only those words from the file TEXT1.TXT which
don’t start with an uppercase vowel (i.e.with ‘A’,‘E’,‘I’,‘O’,‘U’).
For example, if the file TEXT1.TXT contains
Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain
Carry and When rains
Code:
f=open("TEXT1.TXT","w")
while True:
str=input("Enter Text: ")
if len(str)==0:
break
else:
f.write(str+"\n")
f.close()
def vowelwords():
f=open("TEXT1.TXT","r")
z=open("TEXT2.TXT","w")
newstr=" "
s=f.read()
w=s.split()
for i in w:
if i[0] not in "AEIOU":
newstr+=i+" "
z.write(newstr+"\n")
z.close()
z=open("TEXT2.TXT","r")
u=z.read()
print(u)
f.close()
vowelwords()
Output:
Enter Text: JIyaa
Enter Text: And
Enter Text: Radhikaa
Enter Text: Or
Enter Text: Navodita
Enter Text:
Jiyaa Radhikaa Navodita
>>>