Program to check a string is palindrome or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string s; we have to check whether it is a palindrome or not. As we know a palindrome is when the word is the same forwards and backwards.

So, if the input is like s = "racecar", then the output will be True

To solve this, we will follow these steps −

  • t := reverse of s
  • if t is same as s, then
    • return True
  • otherwise,
    • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      t=s[::-1] if t==s:
      return True else :
   return False
ob = Solution()
print(ob.solve("racecar"))

Input

"racecar"

Output

True
raja
Published on 05-Oct-2020 10:58:03
Advertisements