Remove final characters from string recursively - What's the best way to do this?
<p>I am reading lines from a file one at a time and before i store each, i wanna modify them according to the following simple rule:</p>

<ul>
<li>if the last character is not any of, e.g., <code>{'a', 'b', 'c'}</code> store the line.</li>
<li>if that is not the case, remove the character (pop-like) and check again.</li>
</ul>

<p>What i currently have (felt like the obvious thing to do) is this:</p>

<pre><code>bad_chars = {'a', 'b', 'c'}


def remove_end_del(line_string, chars_to_remove):
    while any(line_string[-1] == x for x in chars_to_remove):
        line_string = line_string[:-1]
    return line_string

example_line = 'jkhasdkjashdasjkd|abbbabbababcbccc'
modified_line = remove_end_del(example_line, bad_chars)
print(modified_line)  # prints -&gt; jkhasdkjashdasjkd|
</code></pre>

<p>Which of course works, but the string slicing\reconstruction seems a bit too excessive to my untrained eyes. So i was wondering a couple of things:</p>

<ol>
<li>is there a better way to do this? like a <code>pop</code> type of function for strings?</li>
<li>how is <code>rstrip()</code> or <code>strip()</code> in general implemented? is it also with a <strong>while</strong>?</li>
<li>would it be worthwhile making <code>rstrip()</code> recursive for this example?</li>
<li>Finally, how much better is the following:</li>
</ol>

<hr>

<pre><code>def remove_end_del_2(line_string, chars_to_remove):
    i = 1
    while line_string[-i] in chars_to_remove:
        i += 1
    return line_string[:-i+1]
</code></pre>

<p>Any comment on any of the points made above would be appreciated âº.</p>

<p><strong>Note: the separator ("|") is only there for visualization.</strong></p>

<p>I understand what you mean by <em>excessive</em>, but I think in general, it looks good. The alternative would be to work with indices, which is not very readable. (I also happen to think that regexes are not very readable either...)</p>

<p>You could, however, use a <code>memoryview</code> if you have a <code>bytes</code> object which may or may not be relevant:
<a href="https://docs.python.org/3/library/stdtypes.html#memoryview" rel="nofollow">https://docs.python.org/3/library/stdtypes.html#memoryview</a></p>

<p><strong>1. <code>pop</code> function for strings</strong></p>

<p>No there are no <code>.pop</code> method for <code>str</code>. You'd have to use <code>list(line_string).pop()</code>, where <code>list(s)</code> creates a list with each character of the string as an element.</p>

<p><strong>2. <code>(r)strip</code> implementation</strong></p>

<p>This is probably implemented with a while, yes.
It should be all C code though.</p>

<p><strong>3. recursive <code>rstrip</code></strong></p>

<p>First of all, why would you need to make it <em>recursive</em>?
Second, I think that (recursiveness) would make the mental load unnecessarily high -- so, no.</p>

<p><strong>4. Finally, how much better is the following:</strong></p>

<p>Measure it! Surely it would be faster.</p>

python