How to Convert a Python String to Alternate Cases?


In this article, the user will learn how to convert a Python string to alternate cases. The strings are comprised of characters and it is indicated either within single quotes or double quotes. The string can be converted into alternate cases using the Python language. In the given string, the strings can be converted from uppercase to lowercase or vice versa. The three approaches are demonstrated in this article. The first approach describe the concept of for loop, the second approach uses the join() method and the three approach uses the regular expression to convert a Python string to Alternate Cases.

Approaches

Approach 1 − Utilizing for loop

Approach 2 − Utilizing join() method

Approach 3 − Utilizing regular expression

Approach 1: Python Program to convert a Python string to alternate cases using for loop

For loop is used to iterate through the elements of the string and based on the range and length of the string, it returns the lower or uppercase characters.

Algorithm

  • Step 1 − The string is initialized with a string value.

  • Step 2 − Then the empty string is declared.

  • Step 3 − for loop is used to iterate through the length of the string.

  • Step 4 − When the index of the character is even, it returns the string in lowercase.

  • Step 5 − When the index of the character is odd, it returns the string in uppercase.

  • Step 6 − Then the printing statement will return the converted alternate cases.

Example

#the str is initialized with a string value
str_1 = "Welcome to Tutorialspoint"
#empty string is initialized
new_str = ""
#for loop is used to iterate through the loop along with the range of the length of list
for a in range(len(str_1)):
   if a % 2 == 0:
      #it returns the lowercase of the string when it is even
      new_str += str_1[a].lower()
   else:
      #it returns the uppercase of the string when it is odd
      new_str += str_1[a].upper()
#return the string after converting it into another case
print("The string after alternating case is:",new_str)

Output

The string after alternating case is: wElCoMe tO TuToRiAlSpOiNt

Approach 2: Python Program to convert a Python string to alternate cases using join() method

The join() method is declared inside the empty string and based on the length of the string, it returns the lower or uppercase characters.

Algorithm

  • Step 1 − Step 1: The string is initialized with a string value.

  • Step 2 − Then the empty string is declared using the join() method.

  • Step 3 − for loop is used to iterate through the length of the string.

  • Step 4 − When the character’s index is even, it returns the string in lowercase.

  • Step 5 − When the character’s index is odd, it returns the string in uppercase.

  • Step 6 − Then the printing statement will return the converted alternate cases.

Example

#the str is initialized with a string value
str_1 = "Welcome to Tutorialspoint"
#empty string is initialized with an join() function 
#it returns the lowercase of the string when it is even
new_str = ''.join([str_1[a].lower() 
if a % 2 == 0 
else 
#it returns the uppercase of the string when it is odd
str_1[a].upper() 
#for loop is used to iterate through the loop along with the range of the length of list
for a in range(len(str_1))])
#return the string after converting it into another case
print("The string after alternating case is:", new_str)

Output

The string after alternating case is: wElCoMe tO TuToRiAlSpOiNt

Approach 3: Python Program to convert a Python string to alternate cases using regular expression

The re.sub() function is declared inside the empty string and based on the length of the string, it returns the lower or uppercase characters.

Algorithm

  • Step 1 − The re module is imported to use the re.sub() function.

  • Step 2 − The string is initialized with a string value.

  • Step 3 − Then the empty string is declared using the lambda method.

  • Step 4 − The key parameter is used to convert each string into lowercase or uppercase.

  • Step 5 − When the index of the character is even, it returns the string in lowercase.

  • Step 6 − When the index of the character is odd, it returns the string in uppercase.

  • Step 7 − Then the printing statement will return the converted alternate cases.

Example

#importing the re module to use the lambda function
import re

#the str is initialized with a string value
str_1 = "Welcome to Tutorialspoint"
#empty string is initialized with a regular expression using the key parameter 
#it returns the lowercase of the string when it is even
new_str = re.sub(r"(.)", lambda a: a.group(1).lower() 
if a.start() % 2 == 0 
else 
#it returns the uppercase of the string when it is odd
a.group(1).upper(), str_1)
#return the string after converting it into another case
print("The string after alternating case is:", new_str)

Output

The string after alternating case is: wElCoMe tO TuToRiAlSpOiNt

Conclusion

Python is popular worldwide because of its simplicity and flexibility with other applications. Apart from the other languages it has got its unique place because of its advantages like it’s easy to code with simple syntax and statements. It is being implemented in top companies like Google, NASA and Youtube.

Updated on: 25-Aug-2023

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements