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>One way to do this would be by removing instances of your sub string and looking at the length...</p>

<pre><code>def nofsub(s,ss):
    return((len(s)-len(s.replace(ss,"")))/len(ss))
</code></pre>

<p>alternatively you could use re or regular expressions,</p>

<pre><code>from re import *
def nofsub(s,ss):
    return(len(findall(compile(ss), s)))
</code></pre>

<p>finally you could count them manually,</p>

<pre><code>def nofsub(s,ss):
    return(len([k for n,k in enumerate(s) if s[n:n+len(ss)]==ss]))
</code></pre>

<p>Test any of the three with...</p>

<pre><code>&gt;&gt;&gt; nofsub("asdfasdfasdfasdfasdf",'asdf')
5
</code></pre>

<p>Now that you can count any given character you can iterate through your string's unique characters and apply a counter for each unique character you find. Then sort and print the result.</p>

<pre><code>def countChars(s):
    s = s.lower()
    d = {}
    for k in set(s):
        d[k]=nofsub(s,k)
    for key, value in sorted(d.iteritems(), key=lambda (k,v): (v,k)):
        print "%s: %s" % (key, value)
</code></pre>

python list count