shiquann shiquann -3 years ago 146
Python Question

How do i get my code to return the the answer without the extra '' and ()

How do i get my code to return the the answer without the extra

''
and
()
?

def analyze_text(text):
#removes all of the non-al charaters
new_string = ""
for eachLetter in text:
if eachLetter.isalpha():
new_string += eachLetter
#count the number of e
text_count_E = text.count("E")
text_count_e = text.count("e")
total_e = text_count_E + text_count_e
#gets the percentage of e
percentage = 100 * total_e / len(new_string)

return "The text contains ",len(new_string),"alphabetic characters, of
which",total_e,"(",percentage,")","are 'e'"

Answer Source

Pls, provide a more readable code listing the next time.

You can simply use format() method that each string object can perform. This may be helpful. https://pyformat.info/#simple

In your case it will be something like this:

return "The text contains {} alphabetic characters, of which {} ({}) are 'e'".format(len(new_string), total_e, percentage)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download