March 4, 2017
Palindrome in python
String palindrome in python
Write python code to find palindrome strings. A Palendrome string can be read the same from either direction left to right or right to left.
Palindrome python code
Here is a palindrome algorithm python code. You may also checking this post for a C++ implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# Palindrome function in python def isPalindrome(str): # Convert string to lower case str = str.lower() # Compare the first character with the last # character and the second character with # the one before the last character and so # on but you need to pay attention to the # fact that comparison can not be done before # converting the string to either lower case # letters or upper case letters. for i in range(len(str)/2): # If one difference is found then # it is not a Palindrome if (str[i] != str[len(str)-i-1]): return False # Otherwise it is Palindrome return True # Example string str = "Rats live on no evil star" # Call function if isPalindrome(str): print "String is Palindrome" else: print "String is NOT Palindrome" |
Thanks for visiting. Please use the comments section below for feedback.