How do you generate all combinations of lower and upper characters in a word? e.g:
'abc'
→ ['abc', 'ABC', 'Abc', 'ABc', 'aBC', 'aBc', 'abC', 'Abc']
'ab'
→ ['ab', 'AB', 'Ab', 'aB']
How do you generate all combinations of lower and upper characters in a word? e.g:
'abc'
→ ['abc', 'ABC', 'Abc', 'ABc', 'aBC', 'aBc', 'abC', 'Abc']
'ab'
→ ['ab', 'AB', 'Ab', 'aB']
You can achieve this by zipping the upper and lower case letters and taking their cartesian product:
import itertools
chars = "abc"
results = list(map(''.join, itertools.product(*zip(chars.upper(), chars.lower()))))
print(results)
>>>['ABC', 'ABc', 'AbC', 'Abc', 'aBC', 'aBc', 'abC', 'abc']
To visualise how this works:
zip
is creating our 3 'axes' for us, each with 2 points (the upper / lower cases)[('A', 'a'), ('B', 'b'), ('C', 'c')]
.product
takes the cartesian product of these axes, i.e. the 8 possible coordinates corresponding to the corners of the unit cube it creates: