w3resource

Python: Alternate the case of each letter in a given string and the first letter must be uppercase

Python Basic - 1: Exercise-134 with Solution

Write a Python program that alternates the case of each letter in a given string, with the first letter in the string being uppercase.

Sample Solution

Python Code:

# Define a function 'test' that alternates the case of each letter in a given string.
def test(txt):
    # Initialize an empty string to store the result.
    result_str = ""
    
    # Initialize a boolean variable 's' to determine whether to convert to uppercase or lowercase.
    s = True
    
    # Iterate through each character in the input string 'txt'.
    for i in txt:
        # If 's' is True, convert the character to uppercase; otherwise, convert it to lowercase.
        result_str += i.upper() if s else i.lower()
        
        # Toggle the value of 's' if the character is alphabetic.
        if i.isalpha():
            s = not s
    
    # Return the final result string with alternating case.
    return result_str

# Test cases with different input strings.
str1 = "Python Exercises"
print("Original string: ", str1)
print("After alternating the case of each letter of the said string:")
print(test(str1))

str1 = "C# is used to develop web apps, desktop apps, mobile apps, games and much more."
print("\nOriginal string: ", str1)
print("After alternating the case of each letter of the said string:")
print(test(str1))

Sample Output:

Original string:  Python Exercises
After alternating the case of each letter of the said string:
PyThOn ExErCiSeS

Original string:  C# is used to develop web apps, desktop apps, mobile apps, games and much more.
After alternating the case of each letter of the said string:
C# iS uSeD tO dEvElOp WeB aPpS, dEsKtOp ApPs, MoBiLe ApPs, GaMeS aNd MuCh MoRe.

Explanation:

Here is a breakdown of the above Python code:

  • Define Test Function (test function):
    • The "test()" function takes a string as input and alternates the case of each letter.
    • It initializes an empty string (result_str) to store the final result.
    • A boolean variable 's' is used to determine whether to convert the next character to uppercase or lowercase.
    • The function iterates through each character in the input string, converting them accordingly and toggling the value of 's' for alphabetic characters.

Flowchart:

Flowchart: Python - Alternate the case of each letter in a given string and the first letter must be uppercase.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to compute the sum of the negative and positive numbers of an array of integers and display the largest sum.
Next: Write a Python program to get the Least Common Multiple (LCM) of more than two numbers. Take the numbers from a given list of positive integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.