Program to check string is palindrome with lowercase characters or not in Python

Suppose we have an alphanumeric string s that can hold both uppercase and lowercase letters. We need to check whether s is a palindrome considering only the lowercase alphabet characters.

So, if the input is like s = "rLacHEec0a2r8", then the output will be True because the string contains "racecar" in lowercase, which is a palindrome.

Algorithm

To solve this, we will follow these steps ?

  • Extract all lowercase characters from the string

  • Check if the extracted string equals its reverse

  • Return True if palindrome, False otherwise

Example

Let us see the following implementation to get better understanding ?

def solve(s):
    x = ""
    for i in s:
        if i.islower():
            x += i
    
    return x == x[::-1]

s = "rLacHEec0a2r8"
print(solve(s))

The output of the above code is ?

True

How It Works

The algorithm first extracts only lowercase characters from "rLacHEec0a2r8", which gives us "racecar". Then it checks if "racecar" equals its reverse "racecar", which is true, so the function returns True.

Alternative Approach Using List Comprehension

We can make the solution more concise using list comprehension ?

def solve(s):
    lowercase_chars = ''.join([char for char in s if char.islower()])
    return lowercase_chars == lowercase_chars[::-1]

s = "rLacHEec0a2r8"
print(solve(s))
print(f"Extracted lowercase: {''.join([char for char in s if char.islower()])}")
True
Extracted lowercase: racecar

Testing with Different Examples

def solve(s):
    x = ""
    for i in s:
        if i.islower():
            x += i
    
    return x == x[::-1]

# Test cases
test_strings = ["rLacHEec0a2r8", "AbCdE", "aAbBaA", "XyZzYx"]

for test in test_strings:
    lowercase_only = ''.join([c for c in test if c.islower()])
    result = solve(test)
    print(f"String: {test}")
    print(f"Lowercase only: '{lowercase_only}'")
    print(f"Is palindrome: {result}")
    print("-" * 30)
String: rLacHEec0a2r8
Lowercase only: 'racecar'
Is palindrome: True
------------------------------
String: AbCdE
Lowercase only: 'bde'
Is palindrome: False
------------------------------
String: aAbBaA
Lowercase only: 'aba'
Is palindrome: True
------------------------------
String: XyZzYx
Lowercase only: 'yzzy'
Is palindrome: True
------------------------------

Conclusion

This solution efficiently checks if lowercase characters in a string form a palindrome by extracting them first and then comparing with the reverse. The time complexity is O(n) where n is the length of the string.

Updated on: 2026-03-26T15:53:13+05:30

910 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements