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