class MyStr(str):
    def __init__(self, s):
        self.s = s

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

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

    def __repr__(self):
        return self.s

s1 = input()
s2 = input()

s1 = MyStr(s1)
s2 = MyStr(s2)

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

示例输入:

abcdefg
acdi

示例输出:

abcdefgi
befg
Python 字符串加减运算:自定义 MyStr 类实现字符串的增删操作

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

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