I need to know if there is a function that detects the lowercase letters in a string. Say I started writing this program:
s = input('Type a word')
Would there be a function that lets me detect a lowercase letter within the string s? Possibly ending up with assigning those letters to a different variable, or just printing the lowercase letters or number of lowercase letters.
While those would be what I would like to do with it I'm most interested in how to detect the presence of lowercase letters. The simplest methods would be welcome, I am only in an introductory python course so my teacher wouldn't want to see complex solutions when I take my midterm. Thanks for the help!
You can use built-in function
any
and generator.Prints:
Or you can use a list comprehension / generator expression:
Prints:
There are 2 different ways you can look for lowercase characters:
Use
str.islower()
to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:You could use a regular expression:
The first method returns a list of individual characters, the second returns a list of character groups:
There are many methods to this, here are some of them:
Using the predefined function character.islower():
Using the ord() function to check whether the ASCII code of the letter is in the range of the ASCII codes of the lowercase characters:
Checking if the letter is equal to it's lowercase:
But that may not be all, you can find your own ways if you don't like these ones: D.
Finally, let's start detecting:
You should use
raw_input
to take a string input. then useislower
method ofstr
object.Just do -
and you will find
islower
and other attributes ofstr
To check if a character is lower case, use the
islower
method ofstr
. This simple imperative program prints all the lowercase letters in your string:Note that in Python 3 you should use
print(c)
instead ofprint c
.To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:
Or to get a string you can use
''.join
with a generator: