Reputation: 303
For example we have a string s = "stackoverflow" .if we delete one character then we get "tackoverflow", "sackoverflow", "stckoverflow" ... and so on. How to get all possible strings when we delete k number of characters where k < len(s).
We can do by for loop for given exact number of characters to delete and string.But how to do it when number of characters to delete is not fixed.
Upvotes: 0
Views: 156
Reputation: 13498
itertools
is your friend:
from itertools import combinations
s = "stack overflow"
n_delete = 1
print([''.join(i) for i in combinations(s, len(s) - n_delete)])
Upvotes: 3