Python 字符串加减运算实现:自定义类扩展字符串功能
class NewStr(str): def add(self, other): new_str = '' for char in other: if char not in self: new_str += char return self + new_str
def __sub__(self, other):
new_str = ''
for char in self:
if char not in other:
new_str += char
return new_str
string1 = NewStr(input()) string2 = NewStr(input())
print(string1 + string2) print(string1 - string2)
原文地址: https://www.cveoy.top/t/topic/nZFr 著作权归作者所有。请勿转载和采集!