Professional Documents
Culture Documents
Revision Text File - Answers
Revision Text File - Answers
2. Count number of Vowels, Consonants, Spaces, Digits and Special characters from a text
file.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
vowel = 0
consonant = 0
space = 0
digit = 0
special = 0
for i in content:
if i.isalpha():
if i in 'aeiouAEIOU':
vowel += 1
else:
consonant += 1
elif i == ' ':
space += 1
elif i.isdigit():
digit += 1
else:
special += 1
print('Vowel:', vowel)
print('Consonant:', consonant)
print('Space:', space)
print('Digit:', digit)
print('Special:',special)
f.close()
textFile()
3. Print the words that begin vowel
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0] in 'aeiouAEIOU':
print(i)
f.close()
textFile()
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0] in 'HhRr':
print(i)
f.close()
textFile()
5. Print the words that begin and end with the same characters.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0] == i[-1]:
print(i)
f.close()
textFile()
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0].isalpha() and i[-1].isalpha():
if i[0] not in 'aeiouAEIOU' and i[-1] not in 'aeiouAEIOU':
print(i)
f.close()
textFile()
7. Print the words that end with a digit
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[-1].isdigit():
print(i)
f.close()
textFile()
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if len(i) == 3:
print(i)
f.close()
textFile()
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if 'e' in i or 'E' in i:
print(i)
f.close()
textFile()
10. Print the words that has more than 5 characters in it.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if len(i) > 5:
print(i)
f.close()
textFile()
11. Print the words that has more than 2 ‘t’ letters in it.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i.count('t') > 2:
print(i)
f.close()
textFile()
12. Count the number of words present in the file.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
print("No of words in the file:", len(words))
f.close()
textFile()
13. Count the words that begin the K or ‘k’
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
print("No of words in the file:", len(words))
f.close()
textFile()
14. Count the number of ‘this’ present in a file. (THIS, THis, tHIS, all possible combination to
be included)
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
this = 0
for i in words:
if i.lower() == 'this':
this += 1
print("No of this words :", this)
f.close()
textFile()
def textFile():
f = open('sample.txt', 'r')
lines = f.readlines()
print("No of lines in the file:", len(lines))
f.close()
textFile()
22. Count the number of characters present in each line of the file.
Output:
Line 1: 65
Line 2: 42
Line 3: 55
def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
print("Line",lineNumber,':',len(line))
lineNumber +=1
f.close()
textFile()
23. Count the number of words present in each line of the file.
Output:
Line 1: 12
Line 2: 8
Line 3: 11
def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
words = line.split()
print("Line",lineNumber,':',len(words))
lineNumber +=1
f.close()
textFile()
def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
print("Line",lineNumber,':',line.count('e'))
lineNumber +=1
f.close()
textFile()
25. Count the number of ‘the’ words present in each line.
def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
l = line.upper()
words = l.split()
print("Line",lineNumber,':',words.count('THE'))
lineNumber +=1
f.close()
textFile()
26. Print the words that doesn’t have any vowels in it.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
w = i.upper()
if not('A' in w or 'E' in w or 'I' in w or 'O' in w or 'U' in w):
print(i)
textFile()
27. Print the lines that has more than 2 ‘is’ word in it.
def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
l = line.upper()
words = l.split()
if words.count('IS') > 2:
print(line)
f.close()
textFile()
28. Print the lines that has more than 3 four lettered words.
def textFile():
f = open('sample.txt', 'r')
for line in f:
words = line.split()
c = 0
for w in words:
if len(w) == 4:
c += 1
if c > 3:
print(line)
f.close()
textFile()
29. Copy the lines that has the word ‘that’ in it to a new file named “thatword.txt’
def textFile():
f1 = open('sample.txt', 'r')
f2 = open('thatword.txt','a')
for line in f1:
l = line.upper()
words = l.upper()
if 'THAT' in words:
f2.write(line)
f1.close()
f2.close()
textFile()