111

I want to classify a list of string in Python depending on whether they are upper case, lower case, or mixed case

How can I do this?

1
  • 1
    Consider the possibilities of (1) characters that are not letters and (2) letters that have no case at all. Nov 22, 2011 at 9:20

2 Answers 2

198

There are a number of "is methods" on strings. islower() and isupper() should meet your needs:

>>> 'hello'.islower()
True

>>> [m for m in dir(str) if m.startswith('is')]
['isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper']

Here's an example of how to use those methods to classify a list of strings:

>>> words = ['The', 'quick', 'BROWN', 'Fox', 'jumped', 'OVER', 'the', 'Lazy', 'DOG']
>>> [word for word in words if word.islower()]
['quick', 'jumped', 'the']
>>> [word for word in words if word.isupper()]
['BROWN', 'OVER', 'DOG']
>>> [word for word in words if not word.islower() and not word.isupper()]
['The', 'Fox', 'Lazy']
3
  • hi. thanks for the short answer. But how do I classify words which are capitalized? For example: 'Mixed Word'. Seems the 3rd example fits all possible combinations of mixed words, example: "mIxEd WoRD"..
    – Swadhikar
    Feb 18, 2016 at 6:13
  • 12
    'hello'.istitle()
    – Stephen
    May 22, 2016 at 17:22
  • @Stephen it's important to note that istitle() will only evaluate if the string is title case, not whether it's mixed case. It will return false for a string such as 'The quick Brown Fox', as that's not title cased (but is obviously mixed case).
    – bsplosion
    Sep 20, 2021 at 15:41
2

I want to give a shoutout for using re module for this. Specially in the case of case sensitivity.

We use the option re.IGNORECASE while compiling the regex for use of in production environments with large amounts of data.

>>> import re
>>> m = ['isalnum','isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'ISALNUM', 'ISALPHA', 'ISDIGIT', 'ISLOWER', 'ISSPACE', 'ISTITLE', 'ISUPPER']
>>>
>>>
>>> pattern = re.compile('is')
>>>
>>> [word for word in m if pattern.match(word)]
['isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper']

However try to always use the in operator for string comparison as detailed in this post

faster-operation-re-match-or-str

Also detailed in the one of the best books to start learning python with

idiomatic-python

Not the answer you're looking for? Browse other questions tagged or ask your own question.