class MyString(str): def __sub__(self, y): if self.endswith(y): return self[:-len(y)] x = MyString("mailbox") print x - "box"