0

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.

falsetru
  • 357,413
  • 63
  • 732
  • 636
Nexu
  • 35
  • 4
  • http://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python http://stackoverflow.com/questions/11144389/python-string-with-upper-case-and-lower-case-combination – James Sapam Feb 10 '14 at 15:08

1 Answers1

4

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']
falsetru
  • 357,413
  • 63
  • 732
  • 636