We have all seen numbers and strings in which the reverse gives the same characters as the original strings or numbers. These numbers or words are called Palindromes. In mathematics, palindromes can reveal hidden and interesting properties of numbers. In this tutorial, we will discuss how to check for palindrome in Python.
Let’s dive in.
Table of Contents
Palindrome Meaning: What is a Palindrome
A Palindrome is defined as any word, phrase, or number that remains the same when inverted. In other words, the order of the characters still reads the same whether backward or forward. Palindromes are generally classified as Palindrome numbers, Palindrome strings, and Palindrome phrases although palindrome phrases are rare.
Python programs can be written to check for palindromes in strings and in numbers. In this article, you’ll learn how to write such programs. Let’s begin with checking for palindrome numbers in python.
Checking for Palindrome Numbers in Python
As explained earlier, palindrome numbers are numbers that do not change when the order is reversed. They can therefore be seen as a symmetrical number. Let’s take the number 989 for example. If the digits are read backward, it still reads 989. This is a classical example of a symmetrical number of better put, palindrome.
We can write a python program that checks whether or not a number is a palindrome. Although the program can be written in various ways, we will stick to one method for this tutorial. The block of code below checks for palindrome numbers.
## recive a number from the user
input_number = int(input('Enter a number :'))
#store a copy of the number and set the reverse to equal to 0
palindrome = input_number
reverse_number = 0
# calculate the reverse of the number to see if it is a palindrome or not
while input_number > 0:
#extract the last digit of the number
digit = input_number % 10
#append this digit in the reverse number12
reverse_number=reverse_number * 10 + digit
#floor divide the number and we leave the last digit from the number
input_number = input_number // 10
#compare the reverse of the number to the original number
if palindrome == reverse_number:
print("Right, this number is a palindrome!!!")
else:
print("Unfortunately, this number is not a palindrome!!!")
Output:
Enter a number :13
Unfortunately, this number is not a palindrome!!!
Since 13 is not a palindrome, the program is correct.
Let’s now input a palindrome number.
Enter a number :989
Right, this number is a palindrome!!!
Word Palindrome in Python
Checking for word palindrome in Python is relatively easier. This is because there exists a built-in function that reverses the sequences of a string – the reversed() function. Before we write a code that checks for the palindrome, let’s see how the reversed() function works.
for x in reversed('palindrome'):
print(x, end='')
Output:
emordnilap
As seen the function reverses the sequence of characters in the input word. We can now use this function to write a code that checks for palindrome in python.
#recieve an input from the user
input_word = input('Enter a word: ')
#reverse the word characters
reverse_word = ''.join(reversed(input_word))
#check if the word and reverse words are the same and print the corresponding output
if input_word.lower() == reverse_word.lower():
print("The word is a palindrome!!!")
else:
print("Unfortunately, this word is not a palindrome!!!")
Output:
Enter a word: Prad
Unfortunately, this word is not a palindrome!!!
Since ‘Prad’ is not a palindrome, the code is right. Now let’s input a palindrome, say ‘Anna’.
Enter a word: Anna
The word is a palindrome!!!
Conclusion
In this tutorial, you have discovered what a palindrome is; and how a palindrome string or number can be written. In addition, you learned how to check for palindrome in python. First be writing the code from scratch and secondly, by using the reversed() function in python.
FAQ
If a word or number remains the same when it is read forward and backward, that word or number is a palindrome. Palindromes are a mirror of themselves from the middle character.
You can make a string or number a palindrome by simply appending each character from the end of the string or number until you get to the first character.
1. Check if the first and last characters are the same. If not, return False.
2. Increase the first index and reduce the last index by 1. Check if they are the same. If not, return False.
3. Repeat Step 2 until first index > last index. Then, return True.
Since a string is an iterator, you can reverse its order through fancy indexing. If the string was stored in the variable ‘x’. Then if x == x[::-1], x is a palindrome. If not, x is not a palindrome.