a=input('enter a string :')# palindrome in string
b=a[::-1]if a==b:print(a,'is a palindrome')else:print(a,'is not a palindrome')print('a is not equal to b')if a!=b:print(b,'the reverse of', a)#output:--------------------------------------------------------------------------------case-I
# not palindrome
enter a string :12541254isnot a palindrome
a isnot equal to b
4521 the reverse of 1254--------------------------------------------------------------------------------case-II
# palindrme
enter a string :1232112321is a palindrome
num=int(input("Enter a no.:"))
rev=0
num1=num
while num!=0:
rev=rev*10+(num%10)
num=num//10if num1==rev:print(num1," is a palindrome")else:print(num1," is not a palindrome")
n =input("Enter the word and see if it is palindrome: ")#check palindromeif n == n[::-1]:print("This word is palindrome")else:print("This word is not palindrome")print("franco")
Python Program to Check if binary representation is a palindrome
# Function to check if binary representation of# a number is palindrome or notdefbinarypalindrome(num):# convert number into binary
binary =bin(num)# skip first two characters of string# because bin function appends '0b' as# prefix in binary representation of# a number
binary = binary[2:]# now reverse binary string and compare# it with originalreturn binary == binary[-1::-1]# Driver programif __name__ =="__main__":
num =9print binarypalindrome(num)
How to check if a given string is a palindrome, in Python?
"""
This implementation checks whether a given
string is a palindrome. A string is
considered to be a palindrome if it reads the
same forward and backward. For example, "kayak"
is a palindrome, while, "door" is not.
Let n be the length of the string
Time complexity: O(n),
Space complexity: O(1)
"""defisPalindrome(string):# Maintain left and right pointers
leftIdx =0
rightIdx =len(string)-1while leftIdx < rightIdx:# If chars on either end don't match# string cannot be a palindromeif string[leftIdx]!= string[rightIdx]:returnFalse# Otherwise, proceed to next chars
leftIdx +=1
rightIdx -=1returnTrueprint(isPalindrome("kayak"))# Trueprint(isPalindrome("door"))# False
myString ="aba"if myString == myString[::-1]:print("The string '"+ myString +"' is a palindrome")else:print("The string '"+ myString +"' is not a palindrome")
value =input("Enter a Word: ")if value == value[::-1]:print(value)print(value[::-1])print("THIS WORD IS A PALINDROME")else:print(value)print(value[::-1])print("THIS WORD IS NOT A PALINDROME")
# Python3 code to demonstrate# checking a number is palindrome# using str() + string slicing# initializing number
test_number =9669669# printing the original number print("The original number is : "+str(test_number))# using str() + string slicing# for checking a number is palindrome
res =str(test_number)==str(test_number)[::-1]# printing resultprint("Is the number palindrome ? : "+str(res))
# Python3 code to demonstrate# checking a number is palindrome# using math.log() + recursion + list comprehensionimport math
# the recursive function to reversedefrev(num):returnint(num !=0)and((num %10)*(10**int(math.log(num,10)))+
rev(num //10))# initializing number
test_number =9669669# printing the original number print("The original number is : "+str(test_number))# using math.log() + recursion + list comprehension# for checking a number is palindrome
res = test_number == rev(test_number)# printing resultprint("Is the number palindrome ? : "+str(res))
# Recursive function to check if a# string is palindromedefisPalindrome(s):# to change it the string is similar case
s = s.lower()# length of s
l =len(s)# if length is less than 2if l <2:returnTrue# If s[0] and s[l-1] are equalelif s[0]== s[l -1]:# Call is palindrome form substring(1,l-1)return isPalindrome(s[1: l -1])else:returnFalse# Driver Code
s ="MalaYaLam"
ans = isPalindrome(s)if ans:print("Yes")else:print("No")
defis_palindrome(input_string):# Create two strings, to compare them
new_string =""
reverse_string =""# Traverse through each letter of the input stringfor x inrange(len(input_string)):# This will give x the value of all the possible index numbers for each iteration# Add any non-blank letters to the # end of one string, and to the front# of the other string. if input_string[x]!=" ":
new_string += input_string[x]
reverse_string = new_string[::-1]# Compare the stringsif new_string.lower()== reverse_string.lower():returnTruereturnFalseprint(is_palindrome("Never Odd or Even"))# Should be True