-1

I need this code to ignore (not replace) spaces. Basically, it should capitalise every second letter of the alphabet only.

def spaces(test_space):
    text = (str.lower, str.upper)
    return ''.join(text[i%2](x) for i, x in enumerate(test_space))

print(spaces('Ignore spaces and other characters'))
print(spaces('Ignore spaces and 3rd characters!'))

Output

iGnOrE SpAcEs aNd oThEr cHaRaCtErS

iGnOrE SpAcEs aNd 3Rd cHaRaCtErS
khelwood
  • 55,782
  • 14
  • 81
  • 108
Sekai
  • 11
  • 1
  • Please format your question properly and you can use the ASCII value to skip spaces – Srikrishna Sharma Feb 22 '21 at 15:10
  • 1
    This is same as https://stackoverflow.com/questions/17865563/capitalise-every-other-letter-in-a-string-in-python – Srikrishna Sharma Feb 22 '21 at 15:14
  • "capitalise every second letter of the alphabet only" how is `spaces` then `SpAcEs`, why is it not `sPaCeS` as `P` is the second letter? – python_user Feb 22 '21 at 15:23
  • 2
    @python_user I think he's showing what his program currently outputs. I agree it would be nice if he also showed his desired output. – Evan Rosica Feb 22 '21 at 15:25
  • Does this answer your question? [Capitalise every other letter in a string in Python?](https://stackoverflow.com/questions/17865563/capitalise-every-other-letter-in-a-string-in-python) – buddemat Feb 22 '21 at 16:03

2 Answers2

1

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:

  1. 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).
  2. Capitalize every other character using your current program
  3. Replace each occurrence of '$$' with a space.
  4. 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.

Evan Rosica
  • 1,182
  • 1
  • 12
  • 22
1
def spaces(test_space):
    return " ".join(
        [
            "".join(
                char.upper() if i % 2 == 1 else char.lower()
                for i, char in enumerate(word)
            )
            for word in test_space.split()
        ]
    )

outputs

iGnOrE sPaCeS aNd oThEr cHaRaCtErS
tbjorch
  • 1,544
  • 1
  • 8
  • 21
  • very nice use of a list comprehension, but isn't it supposed to ignore spaces? So the correct output should be: iGnOrE sPaCeS aNd OtHeR cHaRaCtErS – Evan Rosica Feb 22 '21 at 15:33
  • 1
    @EvanRosica now it works as you notes. A little bit messier though, since i had to join each word inside the comprehension into individual words as well as join the return value with spaces between them. – tbjorch Feb 22 '21 at 16:00
  • 1
    Since the comprehension becamse quite messy i formatted it with black and edited answer to let black decide about readability :P – tbjorch Feb 22 '21 at 16:02