Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

String Case Alteration - Solution Explanation

Explore three solutions for altering the case of characters in a string.


Method 1
def solution(s):
result = ""
for i in range(len(s)): # Iterate through the length of the string
if i % 2 == 0: # Even index character to lowercase
result += s[i].lower() # Convert to lowercase
else: # Odd index character to uppercase
result += s[i].upper() # Convert to uppercase
return result

This function solution takes the input string s and returns a new string where characters at even indices are in lowercase, and characters at odd indices are in uppercase.


Example Usage

Input/Output Example
input_string = "CoDeFrIeNds"

result = solution(input_string)
print(result) # Output: "cOdEfRiEnDs"

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.