Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect lowercase letters in Python?

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.

like image 925
JustaGuy313 Avatar asked Oct 17 '12 13:10

JustaGuy313


People also ask

How do you check lowercase letters in python?

islower() In Python, islower() is a built-in method used for string handling. The islower() method returns True if all characters in the string are lowercase, otherwise, returns “False”.

How do you check lowercase letters?

isLowerCase() method can be used to determine if a letter is a lowercase letter. This method takes a char as argument and return a boolean value. If it returns true it means the character is in lowercase form. And if it returns false it means the character is not in lowercase form.

How do you know if lowercase or uppercase in Python?

The Python upper() method converts all lowercase letters in a string to uppercase and returns the modified string. The Python isupper() returns true if all of the characters in a string are uppercase, and false if they aren't.

How do you check if a string is lowercase?

Traverse the string character by character from start to end. Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. If the ASCII value lies in the range of [97, 122], then it is a lowercase letter.


2 Answers

To check if a character is lower case, use the islower method of str. This simple imperative program prints all the lowercase letters in your string:

for c in s:     if c.islower():          print c 

Note that in Python 3 you should use print(c) instead of print c.


Possibly ending up with assigning those letters to a different variable.

To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:

>>> s = 'abCd' >>> lowercase_letters = [c for c in s if c.islower()] >>> print lowercase_letters ['a', 'b', 'd'] 

Or to get a string you can use ''.join with a generator:

>>> lowercase_letters = ''.join(c for c in s if c.islower()) >>> print lowercase_letters 'abd' 
like image 60
Mark Byers Avatar answered Oct 10 '22 07:10

Mark Byers


There are 2 different ways you can look for lowercase characters:

  1. Use str.islower() to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:

    lowercase = [c for c in s if c.islower()] 
  2. You could use a regular expression:

    import re  lc = re.compile('[a-z]+') lowercase = lc.findall(s) 

The first method returns a list of individual characters, the second returns a list of character groups:

>>> import re >>> lc = re.compile('[a-z]+') >>> lc.findall('AbcDeif') ['bc', 'eif'] 
like image 41
Martijn Pieters Avatar answered Oct 10 '22 05:10

Martijn Pieters