Problem Formulation
Given a string s. How to get a Boolean value that indicates whether all characters in the string are lowercase?
Example: Say, you ask the user for input and you want to check whether all input characters are lowercase for further processing:
s = input('your name: ') if <STRING IS LOWERCASE>: print('All characters in your string are lowercase')
If the user types in 'alice'
, the function should return True
whereas it should return False
for the string 'Alice'
or 'ALICE'
.
Simple Solution: string.islower()
Python’s islower()
method is one of the built-in string methods that returns True
if all string characters are lowercase and otherwise False
. For example, the lowercase string 'alice'.islower()
returns True
and the uppercase string 'Alice'.islower()
returns False
.
>>> 'alice'.islower() True
Even if you inject some whitespaces, it’ll still yield a True
value:
>>> 'al ic \ne'.islower() True
And here are some examples that yield a False
value:
>>> 'Alice'.islower() False >>> 'ALICE'.islower() False >>> 'alicE'.islower() False
How to Count the Number of Lowercase Letters in a String?
Problem Formulation: A variant of the problem is to count the number of lowercase letters in a given string.
Examples: Here are three strings and the desired return values.
'alice'
has five (5) lowercase letters.'Alice'
has four (4) lowercase letters.'ALICE'
has zero (0) lowercase letters.
Solution:
You can use the list comprehension statement [c for c in s if c.islower()]
to create a list of lowercase letters in a given string s
. Then pass the resulting list of lowercase characters into Python’s built-in len()
function to obtain the number of lowercase characters in s
.
Here’s the code for our three examples:
def count_lowercase(s): return len([c for c in s if c.islower()]) print(count_lowercase('alice')) # 5 print(count_lowercase('Alice')) # 4 print(count_lowercase('ALICE')) # 0
How to Get a List of All Lowercase Characters in a String?
Problem Formulation: A variant of the problem is to return a list of lowercase letters in a given string.
Examples: Here are three strings and the desired return values.
'alice'
should yield the list['a', 'l', 'i', 'c', 'e']
.'Alice'
should yield the list['l', 'i', 'c', 'e']
.'ALICE'
should yield the empty list[]
.
Solution:
You can use the list comprehension statement [c for c in s if c.islower()]
to create a list of lowercase letters in a given string s
.
Here’s the code for our three examples:
def get_lowercase_chars(s): return [c for c in s if c.islower()] print(get_lowercase_chars('alice')) # ['a', 'l', 'i', 'c', 'e'] print(get_lowercase_chars('Alice')) # ['l', 'i', 'c', 'e'] print(get_lowercase_chars('ALICE')) # []
Alternatives to Check if a String is All Lowercase
There are many ways to Rome—you can solve this problem to check if a string is all lowercase in many different ways.
s = 'alice'
1. Use the predefined str
method islower()
>>> s.islower() False
2. Use the all()
function to check if every letter is lowercase.
>>> all(s.islower() for c in s) True
3. Convert the string to a lowercase string and compare it to the original
>>> s.lower() == s True
4. Use regular expressions and the re.fullmatch()
function combined with character classes.
>>> import re >>> bool(re.fullmatch('[a-z\s]+', s)) True >>> s = 'ALICE IS LOWER' >>> bool(re.fullmatch('[a-z\s]+', s)) False