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.

# 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"
	print "String is NOT Palindrome"

Thanks for visiting. Please use the comments section below for feedback.

Leave a Reply

%d bloggers like this: