2

Is there a way to convert a string to a sequence of uppercase and lowercase letters?

For example, "Kilometers" → "KiLoMeTeRs".

0

2 Answers 2

6
a = 'Kilometers'

print(''.join([char.upper() if i%2==0 else char.lower() for i, char in enumerate(a)]))

result = 'KiLoMeTeRs'

Sign up to request clarification or add additional context in comments.

Comments

2

A more esoteric way:

>>> a = 'Kilometers'
>>> "".join("".join(i) for i in zip(a[::2].upper(), a[1::2].lower()))
'KiLoMeTeRs'

or using @lenik's more concise form:

>>> "".join(a+b for a, b in zip(a[::2].upper(), a[1::2].lower()))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.