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
# 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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
OutputThe 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
# 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))
OutputThe 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.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read