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

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 514

Leave a Reply

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