Python Program to Check Whether a String is a Palindrome or Not – 2024

Hey guys, in this blog we will see a Python Program to Check Whether a String is a Palindrome or Not.

A palindrome is a string that is exactly the same when even reversed. For eg.

emordnilapalindrome = reverse (emordnilapalindrome)

Code

# Python Program to Check Whether a String is a Palindrome or Not

my_str = 'eMordnIlaPaLinDromE'

# make it suitable for caseless comparison
my_str = my_str.lower()
print(my_str)

# reverse the string
rev_str = reversed(my_str)

# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
   print("The string is a palindrome.")
else:
   print("The string is not a palindrome.")

Output

emordnilapalindrome
The string is a palindrome.
  • Here we are simply taking a string.
  • Lowering all the characters of the string for caseless comparison. We have used str.lower() for converting the string to lowercase.
  • Then we are reversing our string and storing it in a variable rev_str.
  • And finally, we are checking if our string is equal to our reversed string.

Check out our other python programming examples

Leave a Reply

Your email address will not be published. Required fields are marked *