alter-case
---
id: alter-case
title: String Case Alteration - Solution Explanation
description: Write a Python function to convert characters at even indices to lowercase and characters at odd indices to uppercase in a given string
tags:
- Python
- String
- Uppercase
- Lowercase
sidebar_position: 7
isPublic: false
---
# String Case Alteration - Solution Explanation
Explore three solutions for altering the case of characters in a string.
<br />
```python title="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.