[Python] Palindrome Checking Function
“One of Lindon’s amusing word-unit palindrome reads: “Girl, bathing on Bikini, eyeing boy, finds boy eyeing bikini on bathing girl.” Other palindromes are symmetric with respect to back-to-front reading letter by letter-“Able was I ere I saw Elba” (attributed jokingly to Napoleon), or the title of a famous NOVA program: “A Man, a Plan, a Canal, Panama.”
Mario Livio, The Equation That Couldn’t Be Solved: How Mathematical Genius Discovered the Language of Symmetry
Palindromes are words, numbers, sentences or the like that read the same way backward and forward. For instance, the number 232 is a palindrome, because no matter if you read it forward or backward, it will always be 232. Similarly, the word ‘madam’ is an English palindrome word, because it reads the same way both backward and forward. Another famous English palindrome is ‘Madam, I’m Adam’, with the sentence being the same, no matter how you read it.
The word ‘palindrome’ is derived from the Greek word ‘palin’ (meaning ‘back’) and dromos (meaning ‘direction’). The whole Greek phrase referred to the backward movement of a crab.
This one is my favorite:
‘Are we not pure? “No, sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man — a prisoner up to new era.’
So, how is it possible to use Python to check if a given word or number is palindrome? Let’s write a function which will help us do so.
"""Econowmics.com""" def Palindrome(n): """This function checks to see if the given entry is palindrome or not""" #The original entry (A number or a word) Original = str(n) #Reversing the entry Reverse = Original[::-1] #Checking to see if the two are equal if Original == Reverse: return True else: return False
The intuition behind this function is very easy. It just receives an input from the user (which is a word or a number), reverses it and checks to see if it equals its reverse or not. Let’s check it with a couple of different inputs: