Python Programing Lab EXERCISE - 5(a)
Aim:
Write a program to count the numbers of characters in the string and store them in a dictionary data structure.
Description:
Traverse each character in the string and its occurrence is stored in the dictionary. Here, We are using a String and a Dictionary.
String:
A String is a sequence of characters. Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.
For reading strings in Python2.7, we are using raw_input(). This function reads a line from input(i.e., the user) and returns a string by stripping a trailing new line.
Dictionary:
The dictionary is Python's built-in mapping type. Dictionaries map keys to values and these <key,value> pairs provides a useful way to store data in python. The only way to access the value part of the dictionary is by using the key.
We can specify the dictionary <key,value> pairs between '{' and '}'. <key,value> pairs arespecified as a list(separated by commas).
Program:
s=input("Enter ur name: ")
#print(len(s)) as length index starts with 1
p=len(s)
count=0
for i in range(0,p):
count=count+1
print(count)
dic=dict([(letter,[]) for letter in s[:]])
print(dic)
Output:
Thank you so much for helping us in every moment sir. I have never seen such a dedicated teacher in my life sir. You are my inspiration sir.
ReplyDelete