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>While Moses's answer is really good, I have a funny feeling this is a homework question and meant for you not to use any imports. Anyways here's an answer with no imports, it's not as efficient as other answers like Moses' or Regex but it works just not as well as others.</p>

<pre><code>MyString = 'x.ABC AND XYZ, \ny.DEF AND Type, \nSome Long String AND Qwerty, \nz.GHI AND Tree \n'
new_string = ''
for each in [[y for y in x.split(' AND ')][0] for x in MyString.split('\n')]:
    new_string+=each
    new_string+='\n'

print(new_string)
</code></pre>

python string