This sounds like homework, so I'm only going to give suggestions and resources not complete code:
One way to do this would be to:
- Replace every space with 2 of some character that can't appear in your text. for example use "$$". This can easily be done via python's
replace
function. We replace a space with 2 characters because each space is "throwing off" the index by one (mod 2), so by replacing each space by two characters corrects the problem (since 2 (mod 2) = 0).
- Capitalize every other character using your current program
- Replace each occurrence of '$$' with a space.
- Put the spaces back using the indexes you saved
Output: iGnOrE sPaCeS aNd 3rD cHaRaCtErS!
Alternatively, you could iterate through the string (using a loop), keeping a position counter, but use a regex to ignore all non-Alphabet characters in the counter. You could also probably accomplish this succinctly via a list comprehension, although it might be more confusing to read.