Counting sub-element in string?
<p>Say I have a <code>string = "Nobody will give me pancakes anymore"</code></p>

<p>I want to count each word in the string. So I do <code>string.split()</code> to get a list in order to get <code>['Nobody', 'will', 'give', 'me', 'pancakes', 'anymore']</code>.</p>

<p>But when I want to know the length of <code>'Nobody'</code> by inputing <code>len(string[0])</code> it only gives me <code>1</code>, because it thinks that the 0th element is just <code>'N'</code> and not <code>'Nobody'</code>. </p>

<p>What do I have to do to ensure I can find the length of the whole word, rather than that single element?</p>

<pre><code>words = s.split()   
d = {word : len(word) for word in words}

or

words = s.split()
d = {}

for word in words:
    if word not in d:
        d[word]=0
    d[word]+=len(word)

In [15]: d  
Out[15]: {'me': 2, 'anymore': 7, 'give': 4, 'pancakes': 8, 'Nobody': 6,   'will': 4}
In [16]: d['Nobody']
Out[16]: 6
</code></pre>

python list string-length