Strip an ordered sequence of characters from a string

I don't know of a built-in way, no, but it's pretty simple:

def strip_string(string, to_strip):
    if to_strip:
        while string.startswith(to_strip):
            string = string[len(to_strip):]
        while string.endswith(to_strip):
            string = string[:-len(to_strip)]
    return string

As of Python 3.9 you can use str.removeprefix and str.removesuffix.

From the docs:

'TestHook'.removeprefix('Test')  # >> 'Hook'
'MiscTests'.removesuffix('Tests')  # >> 'Misc'

I had this same problem when I first started.

Try str.replace instead?

>>> s = 'abcfooabc'
>>> s.replace("abc", "")
0: 'foo'
>>> s.replace("cba", "")
1: 'abcfooabc'
>>> s.replace("acb", "")
2: 'abcfooabc'

I am surprised re.sub wasn't mentioned yet:

>>> re.sub("^abc", "", "abcfooabc") # ^ regex operator matches the beginning of a string
'fooabc'
>>> re.sub("^abc|abc$", "", "abcfooabc") # | string begins with abc or (|) ends with abc
'foo'
>>> re.sub("abc$", "", "abcfooabc") # | string begins with abc or (|) ends with abc
'abcfoo'