I'm being asked to count the occurrence of letters and spaces and punctuation characters in a string from user input and and in the printout the letters should appear in order in which they appear in the text, but no letter should appear twice and also lowercase and uppercase should be counted as one. my code so far looks like this.
S = str(input("Enter a string: "))
S = S.lower()
alpha = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! "
for char in S:
if char in alpha:
count = S.count(char)
print(char,':',count)
output
Enter a string: hello
h : 1
e : 1
l : 2
l : 2
o : 1
i want it to look like this
Enter a string: hello
h : 1
e : 1
l : 2
o : 1
i was thinking if i turned the string and the the count of occurred chars into a nested list like
Enter a string: hello
[['h', 1], ['e', 1], ['l', 2], ['l', 2], ['o', 1]]
could i remove the list that are the same and leave just one?
so i figured out how to do it without using sets and counters.
S = (input("Enter a string: "))
S = S.lower()
alpha = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()! "
L = []
#counts the occurrence of characters in a string.
#then puts them in a list
for char in S:
if char in alpha:
count = S.count(char)
L.append(char)
L.append(count)
#turns list into a nested list
L2 = []
i = 0
while i <len(L):
L2.append(L[i:i+2])
i += 2
#checks that the lists dont appear more than once
L3 = []
for i in L2:
if i not in L3:
L3.append(i)
# print out the letters and count of the letter
for i,k in L3:
print(i,k)
might be long but it works. would like your opinion on it?