In this python dictionary program, we will create a dictionary from a string.
Steps to solve the program
- Take a string as input and create an empty dictionary.
- Use for loop to iterate over characters in the dictionary.
- Add the character as the key and its occurrences in the dictionary as its value to the empty dictionary.
- Use count() to find the occurrences.
- Print the output.
str1 = "sqatools"
dict1 = {}
for char in str1:
dict1[char] = str1.count(char)
print(dict1)
Output :
{'s': 2, 'q': 1, 'a': 1, 't': 1, 'o': 2, 'l': 1}