Python program to check palindrome String

In this post, we will see if String is palindrome or not in Python.
There are many ways to do it.We will see two ways

  • Using python inbuilt function reversed
  • Using iteative method

Using python inbuilt function reversed

Here is simple program to check if string is palindome or not using reversed function.

#Function to check palindrome def checkPalindrome(s): #Make case insensitive s=s.casefold() reversedS=reversed(s) if(list(s)==list(reversedS)): return True else: return False print("is Madam palindrome:",checkPalindrome("MADAM")) print("is Apple palindrome:",checkPalindrome("Apple"))

Output:

is Madam palindrome: True
is Apple palindrome: False

Using iterative method

Here is simple program to check if string is palindome or not using iterative method.We will iterate from both sides and check if both the char are same or not. If both chars are not same then String is not palindrome.

#Iterative Function to check palindore def checkPalindromeItr(s): #Make case insensitive s=s.casefold() index=0 while index < len(s): if s[index]==s[-1-index]: index+=1 return True return False print("is Madam palindrome:",checkPalindromeItr("MADAM")) print("is Apple palindrome:",checkPalindromeItr("Apple"))

Output:

is Madam palindrome: True
is Apple palindrome: False

That’s all about Python Program to Check Whether a String is Palindrome or Not.

Leave a Reply

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