I don't understand why the strip() function returns the way it does below. I want to strip() the last occurence of Axyx. I got around it by using rstrip('Axyx') but what's the explanation for the following?
>>>"Abcd Efgh Axyx".strip('Axyx')
'bcd Efgh '
The string passed to strip
is treated as a bunch of characters, not a string. Thus, strip('Axyx')
means "strip occurrences of A
, x
, or y
from either end of the string".
If you actually want to strip a prefix or suffix, you'd have to write that logic yourself. For example:
s = 'Abcd Efgh Axyx'
if s.endswith('Axyx'):
s = s[:-len('Axyx')]