Write a program to count the numbers of characters in the string and store them in a dictionary data structure. Attach output screenshots also.

Program:-

str=input('Enter a string: ')
dict1={} #dictionary stores values from the string
for i in range(0,len(str)):
    dict1.update({i:str[i]}); #logic

print('Total number of characters in a string is: ',len(str))
print(dict1) #print according to the question

Output:-

When i enter string as DEEPAK CHAUHAN

When i enter string as RAHUL GUPTA

Program Explanation:-

Firstly, there is a string that takes an input from a user. dict1 is a empty dictionary. 
In for loop there is a logic, for loop iterates from 0 to length of string - 1.
In for loop, dict1 is updated means values are stored into the dictionary.

Finally print the number of characters present inside the string and thereby, the dict1 will be printed.

Comments