Check if String is Palindrome in Python



Suppose we have alphanumeric string s. It can hold both uppercase or lowercase letters. We have to check whether s is a palindrome or not 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.

To solve this, we will follow these steps −

  • x := blank string

  • for each character i in s, do

    • if i is in lowercase, then

      • x := x concatenate i

  • return true when x is palindrome, otherwise false

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))

Input

"rLacHEec0a2r8"

Output

True
Updated on: 2021-10-12T11:27:36+05:30

764 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements