MRAB writes:
On 2019-04-02 19:10, Stephen J. Turnbull wrote:
word[len(prefix) if word.startswith(prefix) else 0:]
It could be 'improved' more to:
word[word.startswith(prefix) and len(prefix) : ]
Except that it would be asymmetric with suffix. That probably doesn't matter given the sequence[:-0] bug. BTW thank you for pointing out that bug (and not quoting the code where I deliberately explicitly introduced the null suffix! ;-) This works: word[:-len(suffix) or len(word)] if word.endswith(suffix) else word Do tutorials mention this pitfall with computed indicies (that -0 is treated as "beginning of sequence")? (I should check myself, but can't spend time this week and so probably won't. :-( )
prefix. So that's the one I'd go with, as I can't think of any applications where multiple copies of the same string would be useful.
_Neither_ version copies if the word doesn't start with the prefix. If you won't believe me, test them! :-)
Oh, I believe you. It just means somebody long ago thought more deeply about the need for copying immutable objects than I ever have.