My question is similar, but different from the following:
How do I remove a substring from the end of a string in Python?
Suppose we have:
input = "baabbbbb_xx_ba_xxx_abbbbbba"
We want to want to keep everything except the ba
at the end and ba
at the beginning.
1) Direct strip()
fails
strip
treats the string as a set. That is, strip
will remove the letters a
and b
appearing in any order. We want to only remove the characters ba
if they appear in that exact order. Also, unlike strip
, we want only zero or one copies removed from the end of the string. "x\n\n\n\n".strip() will remove many new-lines, not just one.
input = "baabbbbb_xx_ba_xxx_abbbbbba"
output = input.strip("ba")
print(output)
prints "_xx_ba_xxx_"
2) Direct replace()
fails
input = "xx_ba_xxx"
output = input.replace("ba", "")
print(output)
# prints `xx__xxx`
Not cool; we only want to remove the sequence "ba
" from the beginning and end of the string, not the middle.
3) Just nope
input = "baabbbbb_xx_ba_xxx_abbbbbba"
output = "ba".join(input.rsplit("ba", 1))
print(output)
# output==input
Final Note
The solution must be general: a function accepting any two input strings, once of which might not be "ba". The undesired leading and trailing strings might contain ".
", "*
" and other characters not nice for use in regular expressions.