Removing parts of a string after certain chars in Python
<p>New to Python.</p>

<p>I'd like to remove the substrings between the word AND and the comma character in the following string:</p>

<pre><code>MyString = ' x.ABC AND XYZ, \ny.DEF AND Type, \nSome Long String AND Qwerty, \nz.GHI AND Tree \n'
</code></pre>

<p>The result should be:</p>

<pre><code>MyString = ' x.ABC,\ny.DEF,\nSome Long String,\nz.GHI\n'
</code></pre>

<p>I'd like to do it without using regex.</p>

<p>I have tried various methods with splits and joins and indexes to no avail.</p>

<p>Any direction appreciated.</p>

<p>Thanks.</p>

<p>Now it is working.. and probably avoiding the 'append' keyword makes it really fast... </p>

<pre><code>In [19]: ',\n'.join([x.split('AND')[0].strip() for x in MyString.split('\n')])


Out[19]: 'x.ABC,\ny.DEF,\nSome Long String,\nz.GHI,\n'
</code></pre>

<p>You can check this answer to understand why...</p>

<p><a href="http://stackoverflow.com/questions/39518899/comparing-list-comprehensions-and-explicit-loops-3-array-generators-faster-than/39519661#39519661">Comparing list comprehensions and explicit loops (3 array generators faster than 1 for loop)</a></p>

python string