Python | Return lowercase characters from given string
Last Updated :
24 Apr, 2023
Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let’s discuss certain ways in which only lowercase letters can be extracted from a string.
Method #1: Using list comprehension + islower()
List comprehension and islower function can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and islower function checks for the lowercase characters.
Python3
test_str = "GeeksForGeeKs "
print ( "The original string is : " + str (test_str))
res = [char for char in test_str if char.islower()]
print ( "The lowercase characters in string are : " + str (res))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using filter() + lambda
Filter function along with lambda functionality can be used to perform this particular task. The filter function performs the specific selection of case characters and the lambda function is used for string traversal.
Python3
test_str = "GeeksForGeeKs "
print ( " The original string is : " + str (test_str))
res = list ( filter ( lambda c: c.islower(), test_str))
print ( " The lowercase characters in string are : " + str (res))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Method #3: Without using islower() builtin method
Python3
test_str = "GeeksForGeeKs"
loweralphabets = "abcdefghijklmnopqrstuvwxyz"
print ( "The original string is : " + str (test_str))
res = []
for i in test_str:
if i in loweralphabets:
res.append(i)
print ( "The lowercase characters in string are : " + str (res))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Method #4: Using ord() method.The ASCII value of the lowercase alphabet is from 97 to 122.
Python3
test_str = "GeeksForGeeKs"
print ( "The original string is : " + str (test_str))
res = []
for i in test_str:
if ord (i) in range ( 97 , 123 ):
res.append(i)
print ( "The lowercase characters in string are : " + str (res))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Method #5 : Using replace() and list() methods
Python3
test_str = "GeeksForGeeKs"
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print ( "The original string is : " + str (test_str))
for i in upperalphabets:
test_str = test_str.replace(i, "")
res = list (test_str)
print ( "The lowercase characters in string are : " + str (res))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Method #6: Using re
This code imports the python regular expression library and uses the method re.match() to check each character in the input string test_str to see if it is lowercase. If the character is lowercase, it is added to a new string, lower_str. The final step is to convert the lower_str to a list of characters and print the result.
Python
import re
test_str = "GeeksForGeeKs"
print ( "The original string is : " + str (test_str))
lower_str = [i for i in test_str if re.match( "[a-z]" , i)]
print ( "The lowercase characters in string are : " + str (lower_str))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Time complexity: O(n)
Auxiliary Space: O(n)
Method 7 : using a for loop and checking if the ASCII value of each character is within the range of lowercase alphabets or not. Here are the steps to follow:
- Initialize an empty list to store the lowercase characters.
- Iterate through each character in the input string using a for loop.
- Check if the ASCII value of the character is within the range of lowercase alphabets (i.e. between 97 and 122).
- If the character is a lowercase alphabet, append it to the list.
- Return the list of lowercase characters.
Python3
test_str = "GeeksForGeeKs "
print ( "The original string is : " + str (test_str))
lowercase_chars = []
for char in test_str:
if 97 < = ord (char) < = 122 :
lowercase_chars.append(char)
print ( "The lowercase characters in string are : " + str (lowercase_chars))
|
Output
The original string is : GeeksForGeeKs
The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Time complexity: O(n), where n is the length of the input string. We iterate through each character in the string exactly once.
Auxiliary space: O(k), where k is the number of lowercase characters in the input string. We store each lowercase character in a list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...