How to count number of occurrences of a chracter in a string (list)
<p>I'm trying to count the occurrence of each character for any given string input, the occurrences must be output in ascending order( includes numbers and exclamation marks)
  I have this for my code so far, i am aware of the Counter function, but it does not output the answer in the format I'd like it to, and I do not know how to format Counter. Instead I'm trying to find away to use the count() to count each character. I've also seen the dictionary function , but I'd be hoping that there is a easier way to do it with count() </p>

<pre><code>from collections import Counter

sentence=input("Enter a sentence b'y: ")
lowercase=sentence.lower()

list1=list(lowercase)
list1.sort()

length=len(list1)
list2=list1.count(list1)
print(list2)

p=Counter(list1)
print(p)
</code></pre>

<p>Your best bet is to use <code>Counter</code> (which does work on a string) and then sort on it's output.</p>

<pre><code>from collections import Counter
sentence = input("Enter a sentence b'y: ")
lowercase = sentence.lower()

# Counter will work on strings
p = Counter(lowercase)
count = Counter.items()
# count is now (more or less) equivalent to
#  [('a', 1), ('r', 1), ('b', 1), ('o', 2), ('f', 1)]

# And now you can run your sort
sorted_count = sorted(count)
# Which will sort by the letter. If you wanted to
#  sort by quantity, tell the sort to use the
#  second element of the tuple by setting key:

# sorted_count = sorted(count, key=lambda x:x[1])

for letter, count in sorted_count:
    # will cycle through in order of letters.
    #  format as you wish
    print(letter, count)
</code></pre>

python list count