Author Topic: Python - Check Palindrome by reversing the string and compare  (Read 1142 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Python - Check Palindrome by reversing the string and compare
« on: December 06, 2011, 05:18:37 pm »
Here is the python source code to reverse the string and then compare to check whether it is a palindrome or not.

Code: (Python)
#Python Source File


# [::-1] is used to reverse the string
def ReverseString(inpstr):
	return inpstr[::-1] 

def IsPalindrome(inpstr):
	revstr = ReverseString(inpstr)
	print (inpstr,"reversed to", revstr) #paranthesis are required for new version
	return inpstr == revstr

Here is how you import and execute the code

>>>import mycode
>>> mycode.IsPalindrome("Kathir")
Kathir reversed to rihtaK
False
>>>quit()

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1