I want to write a program that prompts the root user to enter the word and then capitalizes (converts to a capital letter) every other letter from the word. So if a user enters the word rhinoceros, the program will print rHiNoCeRoS.
Asked
Active
Viewed 1,262 times
-3
-
2Welcome to SO. Check how to ask questions https://stackoverflow.com/help/how-to-ask – CIsForCookies Aug 22 '18 at 07:06
-
`case = ['lower','upper']; print(*(getattr(c, case[i%2])() for i,c in enumerate(s)),sep='')` – juanpa.arrivillaga Aug 22 '18 at 07:11
-
1Or maybe: `case = [str.lower,str.upper]; print(*(case[i%2](c) for i,c in enumerate(s)),sep='')` – juanpa.arrivillaga Aug 22 '18 at 07:12
1 Answers
0
Well, strings are immutable, so you will have to build a new string. One way would str.join
the elements produced by a conditional generator that uppercases characters based on their index:
# inp = input()
inp = 'rhinoceros'
outp = ''.join(c.upper() if i%2 else c for i, c in enumerate(inp))
print(outp)
# 'rHiNoCeRoS'

user2390182
- 72,016
- 6
- 67
- 89