Find all upper, lower and mixed case combinations of a string

I always wanted to try this.

No idea if this fits your qualifications(it does work though).

str = raw_input()

def getBit(num, bit):
   return (num & 1 << bit) != 0

for i in xrange(0,2**len(str)):
   out = ""
   for bit in xrange(0,len(str)):
      if getBit(i,bit):
         out += str[bit].upper()
      else:
         out += str[bit].lower()

   print(out)

The idea is that as you increment in binary, you get every possible permutation of 1s and 0s.

Then you simply convert this list of 1s and 0s to a string, 1 meaning uppercase, 0 meaning lowercase.


import itertools

s = 'Fox'
map(''.join, itertools.product(*zip(s.upper(), s.lower())))
>>> ['FOX', 'FOx', 'FoX', 'Fox', 'fOX', 'fOx', 'foX', 'fox']

Tags:

Python

String