Question

A really simple question:

word = 'toy'

I want to generate following:

word_altered_cases = ['toy', 'Toy', 'tOy', 'toY', 'TOy', 'tOY', 'ToY', 'TOY']

I went this far:

for char in word:
  word.replace(char, char.upper())

But obviously, it will produce incompelete permutation and will replace all chars present in word.

Was it helpful?

Solution

Using itertools.product:

>>> import itertools
>>> word = 'toy'
>>> [''.join(w) for w in itertools.product(*zip(word.lower(), word.upper()))]
['toy', 'toY', 'tOy', 'tOY', 'Toy', 'ToY', 'TOy', 'TOY']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top