-1

I am trying for some time now to imagine a way to generate all possible capitalizations of a word in Python. For instance:

hello
Hello
HEllo
...
heLLo
...
HeLlO

I found some built-in functions which work on capitalization (.capitalize() for instance) but they seem to address typical cases (simple sentences, bibliography). I would be grateful for a hint on where to look as the for loops I thought of, as well as recursion were not a solution (I will not mention them here, they were so wrong)


Clarification: since between 5 and 9 people (via a put on hold, downvote and comments) need a clarification, I will try to make one, using different words.

I have a string made up of characters from a to z (called later word). I would like, in Python, to get a list of the said word, but capitalized in all possible ways (that is, a mixture of upper case letters (like M) and lower case ones (like f). This leads to a list similar to the one above (with variations of the word hello.

I am sure glad I did not ask how to multiply two numbers, I would have need to clarify with a crash course in algebraic rings.

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 2
    What real-life problem is addressed by this question? – Wolf Sep 18 '14 at 15:10
  • This smells like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to do? -- What's your end goal, OP? – amphetamachine Sep 18 '14 at 16:05
  • Not sure what is not clear in my question: having a word, I would like to have an algorythm to get a list of all capitalizations of this word. In Python. The real-life problem is that I want to get that list (because I need it for something completely unrelated to the question). I would be happy to clarify, but I do not know what to further clarify here. – WoJ Sep 18 '14 at 17:35
  • 2 to the power of the amount of the letters in the word? – Huangism Sep 18 '14 at 20:59
  • @Wolf: ok ok, thank you very much. I corrected the typo which as clearly too big of a barrier for some. – WoJ Sep 25 '14 at 10:58
  • Where does this string you have come from? What do you want to do with the permutations of it? – Wolf Sep 25 '14 at 10:59
  • @Wolf: for instance from input, or from a file, or whatever. It does not matter. I have a word in a variable, I want to get a list of the capitalizations of that word. The accepted answer does exactly this. – WoJ Sep 25 '14 at 11:03

2 Answers2

5

Using itertools.product:

import itertools
for chars in itertools.product('hH', 'eE', 'lL', 'lL', 'oO'):
    print(''.join(chars))

OR

word = 'hello'
for chars in itertools.product(*zip(word, word.upper())):
    print(''.join(chars))

output:

hello
hellO
helLo
helLO
heLlo
heLlO
heLLo
heLLO
...
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • +1, Only thing I would add is that this can be written as a one-liner with a list comprehension: `[''.join(l) for l in itertools.product('hH','eE', 'lL', 'lL', 'oO')]` – miku Sep 18 '14 at 15:06
0

When reinterpreting the get of the question, it could also mean:

How to evaluate input from a keybord with a broken shift key (provided case doesn't matter)?

Then, I'd probably try this

import re

m = re.match(user_input, "hello", re.IGNORECASE);

...or just this

user_input.lower() == "hello"
Wolf
  • 9,679
  • 7
  • 62
  • 108