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?

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
shreyas
  • 2,510
  • 4
  • 19
  • 20

2 Answers2

197

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']
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 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 '16 at 06:13
  • 12
    'hello'.istitle() – Stephen May 22 '16 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 '21 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

eleijonmarck
  • 4,732
  • 4
  • 22
  • 24