+ 1

I really can't figure how to add a string or any data type to a dictionary?

Given a string as input, you need to output how many times each letter appears in the string. You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. Create a program to take a string as input and output a dictionary, which represents the letter count. Sample Input hello Sample Output {'h': 1, 'e': 1, 'l': 2, 'o': 1}

7th Dec 2021, 5:20 AM
Musharraf
Musharraf - avatar
3 Réponses
+ 6
Musharraf , here are some basic tasks about dictionaries that are required to solve the exercise: letters = {} # create an empty dict print(letters) letters['a'] = 1 # add a new key 'a' to the dict with the value of 1 print(letters) letters['a'] = letters['a'] +1 # modify the value of an existing key 'a' by increasing it by 1 print(letters) if 'a' in letters: # check if a key already exist print("found") # do something else: print("not found") # do something else
7th Dec 2021, 10:14 AM
Lothar
Lothar - avatar
+ 3
If this is for the project module. You just loop over the letters in the text. (for-in) Check if the letter is in the dictionary. If it is then add 1 to its value. If not then add it to the dictionary with a value of 1. Output the dict after the loop. I suggest you review the for loop section and dictionaries. If you have any further issues, post a link to your code attempt and explain what your issue(s) are.
7th Dec 2021, 7:09 AM
ChaoticDawg
ChaoticDawg - avatar
7th Dec 2021, 6:00 AM
Ipang