Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since this sounds like homework, I'm just going to give you some pointers about what you'll need to do:</p> <ol> <li><p>Find out which charachters are in the <code>text1</code> string. The point is that you wont interate over the same charachter twice.<br> <a href="http://docs.python.org/py3k/library/stdtypes.html#set" rel="nofollow noreferrer"><code>set()</code></a> can help you with that:</p> <pre><code>&gt;&gt;&gt; set('fooled') set(['d', 'e', 'l', 'o', 'f']) </code></pre> <p>Try to play a bit with them.</p></li> <li><p>Iterate over the set of (different) charachters from <code>text1</code>. You can use <a href="http://docs.python.org/py3k/library/stdtypes.html#str.count" rel="nofollow noreferrer"><code>.count()</code></a>:</p> <pre><code>&gt;&gt;&gt; 'hello world'.count('e') 1 &gt;&gt;&gt; 'hello world'.count('o') 2 </code></pre> <p>This can count how many times a charachter occures in a string, you'll need to sum all those values and return that sum.</p></li> </ol> <p><strong>Note:</strong> There are many ways for doing what you're asking this is only one of them (and not the best performant one). If you look around searching for "count string occurences" or something like, that you can find other interesting solutions :)</p> <hr> <p>Another approach could be to to start from <code>text2</code>:</p> <ol> <li>Iterate with a for-loop over <code>text2</code></li> <li>If a char from text2 is found <code>in text1</code>, than increment by one your sum.</li> </ol> <hr> <p><strong>Update:</strong> Have you tried a bit to play with them?</p> <p>Check the difference between:</p> <pre><code>&gt;&gt;&gt; word = 'fooled' &gt;&gt;&gt; for c in word: ... print(c) </code></pre> <p>and:</p> <pre><code>&gt;&gt;&gt; word = 'fooled' &gt;&gt;&gt; for c in set(word): ... print(c) </code></pre> <p>It shouldn't be hard to call inside the for-loop <code>text2.count(c)</code>.</p> <p>If this still <a href="https://stackoverflow.com/questions/9757799/processing-strings-in-a-loop#comment12415475_9758077">"doens't make much sense"</a>, than I'd suggest to read a good <a href="http://docs.python.org/py3k/tutorial/index.html" rel="nofollow noreferrer">Python Tutorial</a> and come back later.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload