Home >>Python Programs >Python program to check if a string is palindrome or not
In this example, we will see a Python program through which we can check if the given string is palindrome or not.
A string is known as palindrome if the string read from left to right is equal to the string read from right to left which means that if the actual string is equal to the reversed string.
We can check the palindrome string using either loops or isPalindrome() function.
Program 1:
# Python program to check if a string is
# palindrome or not
# function to check palindrome string
def isPalindrome(string):
result = True
str_len = len(string)
half_len= int(str_len/2)
for i in range(0, half_len):
# you need to check only half of the string
if string[i] != string[str_len-i-1]:
result = False
break
return result
# Main code
x = "Google"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
x = "ABCDEDCBA"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
x = "RADAR"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
# Python program to check if a string is
# palindrome or not
# function to check palindrome string
def isPalindrome(string):
rev_string = string[::-1]
return string == rev_string
# Main code
x = "Google"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
x = "ABCDEDCBA"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")
x = "RADAR"
if isPalindrome(x):
print(x,"is a palindrome string")
else:
print(x,"is not a palindrome string")