Lecture
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"
Previous lessonCoding Quiz - String Case ConversionNext lessonCoding Quiz - Create a List of Powers of 2
Lessons in this chapter · Introduction to Python Algorithms
- 1. Why You Need to Learn Algorithms?
- 2. Review of Algorithm Complexity Concepts
- 3. Life is Practical! Algorithm Problem Solving
- 4. Coding Quiz - Reverse String
- 5. String Reversal - Solution
- 6. Coding Quiz - String Case Conversion
- 7. String Case Alteration - Solution Explanation
- 8. Coding Quiz - Create a List of Powers of 2
- 9. Creating a Power of Two List - Solution Guide
- 10. Coding Quiz - Sorting Tuples by Score
- 11. Sort Tuples by Score - Solution
- 12. Coding Quiz - Find Union and Intersection
- 13. Calculating Union and Intersection - Problem Solution
- 14. Coding Quiz - Create a Dictionary from Key/Value Pairs
- 15. Create Dictionary from Key/Value Pairs - Solution
- 16. Coding Quiz - Determine Grade Using Conditional Statements
- 17. Calculating Grades with Conditional Statements - Solution
- 18. Coding Quiz - Diagonal Traverse of 2D Array
- 19. Diagonal Traversal of a 2D Array - Problem Solution
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help