Lecture
String Case Conversion - Problem Solution
Explore three methods to solve the problem of converting string cases.
Method 1
def solution(s): result = "" for i in range(len(s)): # Loop through the string length if i % 2 == 0: # Convert even-indexed characters to lowercase result += s[i].lower() else: # Convert odd-indexed characters to uppercase result += s[i].upper() return result
Using this method, you can convert even-indexed characters in the input string s
to lowercase and odd-indexed characters to uppercase, then return the result.
Example Usage
Input and Output Example
input_string = "CoDeFrIeNds" result = solution(input_string) print(result) # Output: "cOdEfRiEnDs"
Mission
0 / 1
What Python function best fills the blank below?
The function that applies the same operation to all elements of a list and returns the results is .
map
filter
reduce
apply
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help