Site icon Machine Learning Projects

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

Machine Learning Projects

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.

Check out our other python programming examples

Exit mobile version