71.8k views
4 votes
Write a function analyze_text that receives a string as input. Your function should count the number of alphabetic characters (a through z, or A through Z) in the text and also keep track of how many are the letter 'e' (upper or lowercase). Your function should return an analysis of the text in the form of a string phrased exactly like this: "The text contains 240 alphabetic characters, of which 105 (43.75%) are ‘e’."

Using function

User Ysakhno
by
7.2k points

1 Answer

2 votes

Answer:

def analyze_text(sentence):

count = 0

e_count = 0

for s in sentence:

s = s.lower()

if s.isalpha():

count += 1

if s == "e":

e_count += 1

return "The text contains " + str(count) + " alphabetic characters, of which " + str(e_count) + " (" + str(e_count*100/count) + "%) are ‘e’."

Step-by-step explanation:

Create a function called analyze_text takes a string, sentence

Initialize the count and e_count variables as 0

Create a for loop that iterates through the sentence

Inside the loop, convert all letters to lowercase using lower() function. Check each character. If a character is a letter, increment the count by 1. If a character is "e", increment the e_count by 1.

Return the count and e_count in required format

User Zaw Than Oo
by
7.1k points