String not in string
<p>So I have two strings</p>

<p><code>a = "abc"</code> and <code>b = "ab"</code></p>

<p>Now I am trying to find the characters in a which are not present in b</p>

<p>The code that I have is : </p>

<pre><code>for element in t: 
    if element not in s: 
        print element
</code></pre>

<p>This is giving some error for large strings. I have not looked into that error yet but I was wondering that another way to do the same thing would be something similar to : </p>

<pre><code>if a not in b:
//further code to identify the element that is not in string b
</code></pre>

<p>The piece of code above gives me <code>False</code> when I run it, I don't know how to identify the element which is not present in the second string.</p>

<p>How do I go about this?</p>

<p>This is the sort of thing that a <code>set</code> is really good for:</p>

<pre><code>&gt;&gt;&gt; a = "abc"
&gt;&gt;&gt; b = "abd"
&gt;&gt;&gt; set(a).difference(b)
set(['c'])
</code></pre>

<p>This gives you items in <code>a</code> that aren't in <code>b</code>.  If you want the items that only appear in one or the other, you can use <code>symmetric_difference</code>:</p>

<pre><code>&gt;&gt;&gt; a = "abc"
&gt;&gt;&gt; b = "abd"
&gt;&gt;&gt; set(a).symmetric_difference(b)
set(['c', 'd'])
</code></pre>

<hr>

<p>Note that your code <em>should</em> work too given proper inputs:</p>

<pre><code>&gt;&gt;&gt; for element in a:
...     if element not in b:
...         print element
... 
c
</code></pre>

<p>However, if you're dealing with large sequences, this is much less efficient and it's a bunch more code to write so I don't really recommend it.</p>

python string python-2.7