Open In App

Python | Alternate cases in String

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 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

Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n), as we are using a res string to store the result.



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 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

Time complexity: O(n), where n is the length of the test_list. The list comprehension takes O(n) time
Auxiliary Space: O(1), constant extra space is required

Method #3 : Using find() method




# 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

Time Complexity: O(n), where n is the length of the input list. This is because we’re using find() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list

Method #4:Using recursion




# 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:




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()




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)

Method#7: Using join() and split().




def alternate_cases(string):
    return ''.join([''.join(char.upper() if i%2 == 0 else char.lower() for i, char in enumerate(word)) for word in string.split()])
 
test_str = "geeksforgeeks"
print("The original string is: ",test_str)
 
res = alternate_cases(test_str)
print("The alternate case string is : " + res)
#This code is contributed by tvsk.

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

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


Article Tags :