Python: Reverse the case of all strings. For those strings, which contain no letters, reverse the strings
Python Programming Puzzles: Exercise-52 with Solution
Write a Python program to reverse the case of all strings. For those strings, which contain no letters, reverse the strings.
Input: ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique'] Output: ['CAT', 'CATATATATCTSA', 'ABCDEFHIJKLMNOP', '521581932952421', '', 'FOO', 'UNIQUE'] Input: ['Green', 'Red', 'Orange', 'Yellow', '', 'White'] Output: ['gREEN', 'rED', 'oRANGE', 'yELLOW', '', 'wHITE'] Input: ['Hello', '!@#', '!@#$', '123#@!'] Output: ['hELLO', '!@#', '!@#$', '123#@!']
Pictorial Presentation:

Sample Solution:
Python Code:
def test(strs: list[str]) -> list[str]:
return [s.swapcase() if any(c.isalpha() for c in s) else s[::-1] for s in strs]
strs = ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
print("Original list:")
print(strs)
print("Reverse the case of all strings. For those strings which contain no letters, reverse the strings:")
print(test(strs))
strs = ['Green', 'Red', 'Orange', 'Yellow', '', 'White']
print("\nOriginal list:")
print(strs)
print("Reverse the case of all strings. For those strings which contain no letters, reverse the strings:")
print(test(strs))
strs = ["Hello", "!@#", "!@#$", "123#@!"]
print("\nOriginal list:")
print(strs)
print("Reverse the case of all strings. For those strings which contain no letters, reverse the strings:")
print(test(strs))
Sample Output:
Original list: ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique'] Reverse the case of all strings. For those strings which contain no letters, reverse the strings: ['CAT', 'CATATATATCTSA', 'ABCDEFHIJKLMNOP', '521581932952421', '', 'FOO', 'UNIQUE'] Original list: ['Green', 'Red', 'Orange', 'Yellow', '', 'White'] Reverse the case of all strings. For those strings which contain no letters, reverse the strings: ['gREEN', 'rED', 'oRANGE', 'yELLOW', '', 'wHITE'] Original list: ['Hello', '!@#', '!@#$', '123#@!'] Reverse the case of all strings. For those strings which contain no letters, reverse the strings: ['hELLO', '#@!', '$#@!', '!@#321']
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Find the first n Fibonacci numbers.
Next: Find the product of the units digits in the numbers.
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