origs = list(s)
alphabet = set(alphabet)
for nsubs in range(minsubs, maxsubs + 1):
    for ix in combinations(list(range(len(s))), nsubs):
        prods = [(alphabet - set(origs[i])) for i in ix]
s = origs[:]
for newchars in product(*prods):
    for i, char in zip(ix, newchars):
        s[i] = char
yield ''.join(s)
count = 0
for s in sub('BBBX', 'BGCX', 0, 2):
    count += 1
print(s)
print(count)
Python - build new string of specific length with n replacements from specific alphabet