class MyString(str): def init(self, s): super().init() self.s = s

def __add__(self, other):
    result = ''
    for c in other:
        if c not in self.s:
            result += c
    return self.s + result

def __sub__(self, other):
    result = ''
    for c in self.s:
        if c not in other:
            result += c
    return result

def __repr__(self):
    return self.s

s1 = MyString(input()) s2 = MyString(input())

print(s1 + s2) print(s1 - s2)

Python 字符串加减法操作 - 自定义类实现

原文地址: https://www.cveoy.top/t/topic/nZFE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录