Friday, November 29, 2024
Google search engine
HomeLanguagesPython | Return lowercase characters from given string

Python | Return lowercase characters from given string

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




# Python3 code to demonstrate working of
# Return lowercase characters in string
# Using list comprehension + islower()
 
# initializing string
test_str = "GeeksForGeeKs "
 
# printing original string
print("The original string is : " + str(test_str))
 
# Return lowercase characters in string
# Using list comprehension + islower()
res = [char for char in test_str if char.islower()]
 
# printing result
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




# Python3 code to demonstrate working of
# Return lowercase characters in string
# Using filter() + lambda
 
# initializing string
test_str = "GeeksForGeeKs "
 
# printing original string
print(" The original string is : "+ str(test_str))
 
# Return lowercase characters in string
# Using filter() + lambda
res = list(filter(lambda c: c.islower(), test_str))
 
# printing result
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




# Python3 code to demonstrate working of
# Return lowercase characters in string
 
# initializing string
test_str = "GeeksForGeeKs"
loweralphabets = "abcdefghijklmnopqrstuvwxyz"
# printing original string
print("The original string is : " + str(test_str))
 
# Return lowercase characters in string
res = []
for i in test_str:
    if i in loweralphabets:
        res.append(i)
# printing result
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




# Python3 code to demonstrate working of
# Return lowercase characters in string
 
# initializing string
test_str = "GeeksForGeeKs"
# printing original string
print("The original string is : " + str(test_str))
 
# Return lowercase characters in string
res = []
for i in test_str:
    if ord(i) in range(97, 123):
        res.append(i)
# printing result
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




# Python3 code to demonstrate working of
# Return lowercase characters in string
 
# initializing string
test_str = "GeeksForGeeKs"
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Return lowercase characters in string
for i in upperalphabets:
    test_str = test_str.replace(i, "")
res = list(test_str)
 
# printing result
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
 
# Initializing string
test_str = "GeeksForGeeKs"
 
# Printing original string
print("The original string is : " + str(test_str))
 
# Using the isLower() method from the python re library to check whether each character in the input string is in lowercase or not
lower_str = [i for i in test_str if re.match("[a-z]", i)]
 
# Printing the result
print("The lowercase characters in string are : " + str(lower_str))
#This code is contributed by Edula Vinay Kumar Reddy


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:

  1. Initialize an empty list to store the lowercase characters.
  2. Iterate through each character in the input string using a for loop.
  3. Check if the ASCII value of the character is within the range of lowercase alphabets (i.e. between 97 and 122).
  4. If the character is a lowercase alphabet, append it to the list.
  5. Return the list of lowercase characters.

Python3




# initializing string
test_str = "GeeksForGeeKs "
 
# printing original string
print("The original string is : " + str(test_str))
 
# Return lowercase characters in string
# Using for loop and ASCII value
lowercase_chars = []
for char in test_str:
    if 97 <= ord(char) <= 122:
        lowercase_chars.append(char)
 
# printing result
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.

RELATED ARTICLES

Most Popular

Recent Comments