Python – Filter Similar Case Strings


When it is required to filter similar case strings, list comprehension can be used along with ‘isupper’ and ‘islower’ methods.

Example

Below is a demonstration of the same

my_list = ["Python", "good", "FOr", "few", "CODERS"]

print("The list is :")
print(my_list)

my_result = [sub for sub in my_list if sub.islower() or sub.isupper()]

print("The strings with same case are :")
print(my_result)

Output

The list is :
['Python', 'good', 'FOr', 'few', 'CODERS']
The strings with same case are :
['good', 'few', 'CODERS']

Explanation

  • A list is defined and is displayed on the console.

  • The list comprehension is used to iterate over the list and check if the strings are lower case or upper case.

  • This result is assigned to a variable.

  • This is displayed as output on the console.

Updated on: 16-Sep-2021

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements