- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements