Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Lowercase and Uppercase String

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

For example, "Kilometers" → "KiLoMeTeRs".

like image 932
Dan A Avatar asked May 21 '26 04:05

Dan A


1 Answers

a = 'Kilometers'

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

result = 'KiLoMeTeRs'

like image 167
SimonR Avatar answered May 23 '26 17:05

SimonR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!