Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mary had a little lamb Whose fleece was white as snow And everywhere that Mary w

ID: 3722019 • Letter: M

Question

Mary had a little lamb Whose fleece was white as snow And everywhere that Mary went The lamb was sure to go! and you run reverse lamb.txt to out lamb.txt then out lamb.txt contains The lamb was sure to go. And everywhere that Mary went Whose fleece was white as snow Mary had a little lamb Hint: You might use loop or simply readline() and write) methods 3. Use attached text file text.txt. Write a program that reads the file's contents and determines the following: . The number of uppercase letters in the file . The number of lowercase letters in the file . The number of digits in the file . The number of whitespace characters in the file Hint: read the file using read) method and count occurrence of each type items from the string using string methods.

Explanation / Answer

Code:
  
# Python programm to read lines from a file
# and copy them to another file in reverse
# order
a = open("lamb.txt","r") #opening the file to be read
b = a.readlines() #reading the whole file into a list
b = b[::-1] #reversing the list
aa = open("lamb_out.txt","a")#opening the file to which we have to copy
for line in b:
aa.write(line) copying the reversed lines to new file
aa.close() #closing the write file
a.close() #closing the read file

lamb.txt file:
Mary had a little lamb
Whose fleece was white as snow
And everywhere that Mary went
The lamb was sure to go!


lamb_out.txt file:
The lamb was sure to go!
And everywhere that Mary went
Whose fleece was white as snow
Mary had a little lamb
____________________________________________________________________________

Code:

# Python program to read a txt file and print the
# number of uppercase, lowercase characters, numbers
# and whitespaces

#Reading the file
a = open("text.txt","r")
b = a.readlines()
#initializing the values to be counted
upper = 0
lower = 0
numbers = 0
whitespaces = 0
for line in b:
#searching in each line for the occurrences
for i in range(len(line)):
ch = line[i]
#counting uppercase letters
if (ch.isupper()):
upper+=1
#counting lowercase letters
if (ch.islower()):
lower+=1
#counting digits
if (ch.isnumeric()):
numbers+=1
#counting whitespaces
if (ch.isspace()):
whitespaces+=1
#printing the final result to the screeen
print("The number of Upper Case letters are: %d The number of Lower Case letters are: %d The number of digits are: %d The number of Whitespaces are: %d "%(upper, lower, numbers, whitespaces))
#closing the file
a.close()
  
  
text.txt file:
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC.

Output:
The number of Upper Case letters are: 7
The number of Lower Case letters are: 117
The number of digits are: 6
The number of Whitespaces are: 29