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:
def test(txt):
result_str = ""
s = True
for i in txt:
result_str += i.upper() if s else i.lower()
if i.isalpha():
s = not s
return result_str
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.
Flowchart:

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.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join