Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : 'w3resource'
Expected output : {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}
Answers
Answered by
2
Answer:
Sorry, didn't see your post
def createDictionary(string):
dictionary = {}
repeats = 0
for char in string:
#for current character:
#counts number of characters in string
numberCharacters = string.count(char)
if numberCharacters == 1:
dictionary[char] = 1
else:
dictionary[char] = numberCharacters
print(dictionary)
createDictionary("w3resouce")
Explanation:
loops through the string and counts the number of times character is in string. Then it assigns dictionary value.
Similar questions
English,
4 months ago
India Languages,
9 months ago
Math,
9 months ago
Math,
1 year ago
English,
1 year ago