How to Check Whether a String is Palindrome or Not using Python?



Use reveresed() function from Python's standard library. It returns reversed object which can be converted in a list object

>>> str1='malayalam'
>>> l1=list(reversed(str1))
>>> l1
['m', 'a', 'l', 'a', 'y', 'a', 'l', 'a', 'm']

Join all characters in a list using join() method

>>> str2=''.join(str(x) for x in l1)

Compare str1 and str2. If they are equal the original string is a palindrome

>>> if str1==str2:
           print ('palindrome')
else:
           print ('not palindrome')

Advertisements