1. Please write the following code in Python 3.2. Please also show all outputs.Create a dictionary named letter_counts that contains each letter and the number of times it occurs in string1. Challenge: Letters should not be counted separately as upper-case and lower-case. Intead, all of them should be counted as lower-case.string1 = "There is a tide in the affairs of men, Which taken at the flood, leads on to fortune. Omitted, all the voyage of their life is bound in shallows and in miseries. On such a full sea are we now afloat. And we must take the current when it serves, or lose our ventures."

Respuesta :

Answer:

string1="There is a tide in the affairs of men, Which taken at the flood, leads on to fortune. Omitted, all the voyage of their life is bound in shallows and in miseries. On such a full sea are we now afloat. And we must take the current when it serves, or lose our ventures."

string1=string1.lower()   #covert whole string to lowercase

letter_counts={}

for ch in string1:

   letter_counts[ch]=letter_counts.get(ch,0)+1   #updates counter for each letter

letter_counts['o']

OUTPUT :

17

Explanation:

lower() is used to change the string to lowercase characters so that uppercase and lowercase is not differentiated for the same letter. After that, letter_counts is declared as a dictionary which is empty. for loop is used in which every letter of string will be stored ch variable. Dictionary in python has a get method in which 2 arguments can be passed, first one is the key whose value to be retrieved and the other argument would be default value to be set if not found and ch in string1 will increment the value of key ch in letter_counts.