Find count of characters within the string in Python
<p>I am trying to create a dictionary of word and number of times it is repeating in string. Say suppose if string is like below</p>

<pre><code>str1 = "aabbaba"
</code></pre>

<p>I want to create a dictionary like this </p>

<pre><code>word_count = {'a':4,'b':3}
</code></pre>

<p>I am trying to use dictionary comprehension to do this.
I did </p>

<pre><code>dic = {x:dic[x]+1 if x in dic.keys() else x:1 for x in str}
</code></pre>

<p>This ends up giving an error saying</p>

<pre><code>  File "&lt;stdin&gt;", line 1
    dic = {x:dic[x]+1 if x in dic.keys() else x:1 for x in str}
                                               ^
SyntaxError: invalid syntax
</code></pre>

<p>Can anybody tell me what's wrong with the syntax? Also,How can I create such a dictionary using dictionary comprehension?</p>