Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Alternate cases in String

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 31 Jan, 2023
Improve Article
Save Article

The problem of case changes a string is quite common and has been discussed many times. Sometimes, we might have a problem like this in which we need to convert the odd character of string to upper case and even positioned characters to lowercase. Let’s discuss certain ways in which this can be performed.

Method #1 : Using upper() + lower() + loop 
This task can be performed in brute force method in a way that we iterate through the string and convert odd elements to uppercase and even to lower case using upper() and lower() respectively.
 

Python3




# Python3 code to demonstrate working of
# Alternate cases in String
# Using upper() + lower() + loop
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using upper() + lower() + loop
# Alternate cases in String
res = ""
for idx in range(len(test_str)):
    if not idx % 2:
        res = res + test_str[idx].upper()
    else:
        res = res + test_str[idx].lower()
 
# printing result
print("The alternate case string is : " + str(res))

Output

The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS

 
Method #2 : Using list comprehension 
This is the shortened one liner approach to this problem. It uses same logic as above but in much more compact way.
 

Python3




# Python3 code to demonstrate working of
# Alternate cases in String
# Using list comprehension
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using list comprehension
# Alternate cases in String
res = [ele.upper() if not idx % 2 else ele.lower()
       for idx, ele in enumerate(test_str)]
res = "".join(res)
 
# printing result
print("The alternate case string is : " + str(res))

Output

The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS

Method #3 : Using find() method

Python3




# Python3 code to demonstrate working of
# Alternate cases in String
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Alternate cases in String
res = ""
loweralpha = "abcdefghijklmnopqrstuvwxyz"
upperalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(0, len(test_str)):
    if(i % 2 == 0):
        res += upperalpha[loweralpha.find(test_str[i])]
    else:
        res += loweralpha[loweralpha.find(test_str[i])]
 
# printing result
print("The alternate case string is : " + str(res))

Output

The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS

Method #4:Using recursion

Python3




# Python3 code to demonstrate working of
# Alternate cases in String
# Using recursion
 
def alternate_cases(s, i=0):
    if i == len(s):
        return ""
    return (s[i].upper() if i % 2 == 0 else s[i].lower()) + alternate_cases(s, i+1)
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using recursion
# Alternate cases in String
res = alternate_cases(test_str)
 
# printing result
print("The alternate case string is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original string is : geeksforgeeks
The alternate case string is : GeEkSfOrGeEkS

This method uses recursion to iterate through the input string and convert each character to upper or lower case as appropriate. The base case of the recursion is when the current index i is equal to the length of the input string, at which point the function returns an empty string.

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using map() and lambda function:

Python3




test_str = "geeksforgeeks"
result = "".join([test_str[i].upper() if i%2==0 else test_str[i].lower() for i in range(len(test_str))])
print(result)

Output

GeEkSfOrGeEkS

Time Complexity: O(n)
Auxiliary Space: O(n)

Method#6: Using join() and zip()

Python3




test_str = "geeksforgeeks"
print("The original string is: ",test_str)
res = "".join("".join(x) for x in zip(test_str[0::2].upper(), test_str[1::2].lower()))
print("The alternate case string is : " + res)
#This code is contributed by Vinay Pinjala.

Output

The original string is:  geeksforgeeks
The alternate case string is : GeEkSfOrGeEk

Time Complexity: O(n)
Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!