w3resource

Python: Reverse the case of all strings. For those strings, which contain no letters, reverse the strings


Reverse Case or Reverse Strings

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#@!']

Visual Presentation:

Python: Reverse the case of all strings. For those strings, which contain no letters, reverse the strings.

Sample Solution:

Python Code:

# Define a function named 'test' that takes a list of strings as input and returns a modified list
def test(strs: list[str]) -> list[str]:
    # Use a list comprehension to create a new list with modified strings
    # If a string contains any alphabetic characters, reverse its case; otherwise, reverse the string
    return [s.swapcase() if any(c.isalpha() for c in s) else s[::-1] for s in strs]

# Create a list of strings named 'strs'
strs = ['cat', 'catatatatctsa', 'abcdefhijklmnop', '124259239185125', '', 'foo', 'unique']
# Print a message indicating the task and the original list
print("Original list:")
# Print the original list of strings
print(strs)
# Print a message indicating the task and the result of the test function applied to 'strs'
print("Reverse the case of all strings. For those strings which contain no letters, reverse the strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Create another list of strings named 'strs'
strs = ['Green', 'Red', 'Orange', 'Yellow', '', 'White']
# Print a message indicating the task and the original list
print("\nOriginal list:")
# Print the original list of strings
print(strs)
# Print a message indicating the task and the result of the test function applied to 'strs'
print("Reverse the case of all strings. For those strings which contain no letters, reverse the strings:")
# Print the result of the test function applied to 'strs'
print(test(strs))

# Create yet another list of strings named 'strs'
strs = ["Hello", "!@#", "!@#$", "123#@!"]
# Print a message indicating the task and the original list
print("\nOriginal list:")
# Print the original list of strings
print(strs)
# Print a message indicating the task and the result of the test function applied to 'strs'
print("Reverse the case of all strings. For those strings which contain no letters, reverse the strings:")
# Print the result of the test function applied to 'strs'
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:

Flowchart: Python - Reverse the case of all strings. For those strings, which contain no letters, reverse the strings.

For more Practice: Solve these Related Problems:

  • Write a Python program that toggles the case of each letter in a list of strings and reverses any string containing no alphabetic characters.
  • Write a Python program to process a list of strings such that for each string containing letters, the case is reversed, and for strings with only digits or symbols, the string is reversed.
  • Write a Python program to alternate between reversing the case and completely reversing the string based on the string’s length.
  • Write a Python program to transform a list of strings by inverting the case for letter-containing strings and reversing others, using map() and lambda functions.

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.



Follow us on Facebook and Twitter for latest update.