Author Topic: Python - Check Palindrome using for loop  (Read 1902 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Python - Check Palindrome using for loop
« on: December 07, 2011, 05:24:59 pm »
Here is the python source code to check whether a given string is a palindrome or not by using for loop.

Code: (Python)
#Python Source File


#mycode.py
#returns true if the given string a palindrome, otherwise false

def IsPalindrome(inpstr):
    i = 0
    count = len(inpstr)
    palindrome = True
    for i in range(0, count - 1):
        if(inpstr[i] != inpstr[count - i - 1]):
            palindrome = False
            break
        i = i + 1
    return palindrome

#end of function

Here is how you import and execute the code

>>>import mycode
>>> mycode.IsPalindrome("Kathir")
False
>>> mycode.IsPalindrome("madam")
True
>>>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