count a character(both uppercase and lowercase) in a string, using a for loop in python
<pre><code>def eCount (s):

    """Count the e's in a string, using for loop.

    Params: s (string)
    Returns: (int) #e in s, either lowercase or uppercase
    """

    # INSERT YOUR CODE HERE, replacing 'pass'
</code></pre>

<p>The professor asks not to change anything above.
I'm trying to accomplish:</p>

<pre><code>count = 0
for e in s:
    count +=1
return count
</code></pre>

<p>It gives:</p>

<pre><code>(executing lines 1 to 17 of "e_count_103.py")

15
</code></pre>

<p>you can try <code>count()</code> like:
<code>return s.lower().count('e')</code></p>

<p>In python 3:</p>

<pre><code>def eCount(s):
    return s.lower().count('e')
s = "helloEeeeeE"
print(eCount(s))
</code></pre>

<p>Output:</p>

<pre><code>7
</code></pre>

<p>You can either lowercase the string or uppercase it's up to you. (For upper case you can use <code>s.upper().count('E')</code>).For detail read <a href="http://www.tutorialspoint.com/python/string_count.htm" rel="nofollow">String count() tutorial</a>.</p>

<p>Or if you want to use <code>for</code> loop then try this ( in python 3):</p>

<pre><code>def eCount(s):
    count = 0
    s = s.lower()
    for i in s:
        if i == 'e':
            count+=1
    return count
s = "helloEeeeeE"
print(eCount(s))
</code></pre>

<p>It gives same output:</p>

<pre><code>7
</code></pre>

<blockquote>
  <p><strong>Note :</strong>  <code>e</code> you used in your code in this statement : <code>for e in s:</code> is not character <code>'e'</code> but variable <code>e</code> so it iterate number of characters in
  the string and every time incrementing the <code>count</code> variable.</p>
</blockquote>

<p>You can also use <em>list comprehension</em> :</p>

<pre><code>def eCount(s):
    return sum([1 for i in s if i == 'e' or i == 'E'])
s = "helloEeeeeE"
print(eCount(s))
</code></pre>

<p>For details about list comprehension you can check this <a href="http://stackoverflow.com/questions/16632124/python-emulate-sum-using-list-comprehension">Python - emulate sum() using list comprehension</a>.</p>

python loops count