Python Program to Check If a Number is Palindrome or Not
Learn via video course

Overview
A palindrome is a special kind of word, phrase, number, or any sequence of characters that reads the same backward as forward. We observe palindromes everywhere in our day-to-day lives.
We can use palindromes to solve various mathematical puzzles and computer science-related problems. There are various ways using which we can check the palindrome in Python.
Let's explore them in this article.
What is a Palindrome?
A palindrome is a special kind of word, phrase, number, or any sequence of characters that remains unchanged upon reversal i.e., it reads the same backward as forward. We can notice palindromes all around us in our day-to-day lives. Like for example the word "dad", the sentence "step on no pets", the number "101", or the date "11/11/11", etc.
Now, I ask you to verify whether a given number is a palindrome. In that case, the first thought that will strike your mind is to find out the reverse of that given number and then compare it with the original number to check whether the reversed one is the same as the original one. Right?
Bingo !!! You have found the algorithm to check the palindrome of any number.
Algorithm to Check Palindrome Number
Now, let's take a look at the algorithm to check palindrome number:
- Step_1: Take the input from the user
- Step_2: Find out the reverse of the given input
- Step_3: Check whether the reversed number generated in Step_2 is the same as the input number. If yes, print "Palindrome" and go to Step_5, else go to Step_4
- Step_4: Print "Not a Palindrome"
- Step_5: END
In the above algorithm, the most important step is Step_2, i.e., to find the reverse of the given input number. This step determines the complexity of the whole program as it's the most time-consuming step.
Now, let's focus on the implementation of the algorithm mentioned above. Various ways are used to help us check the palindrome in Python.
Python Program to Check If a Number is Palindrome or Not Using While Loops
It is an iterative method in which we find out the reverse of the given number by extracting the digits from the number and storing them in reverse order to check the palindrome. In this method, we follow the following steps:
- First, we take the input number n.
- Then, we make a copy of the input by assigning it to the temp variable (temp = n). Since integers are immutable in Python, a copy of variable n is stored in the temp variable.
- Initialize a new variable called reverse. It will be used to store the reverse of the input number.
- Now, we find out the reverse of the input number using the below-mentioned mathematical formula inside a while loop:
Here,
- The digit variable stores the remainder attained by dividing the variable n by 10.
- This remainder (digit) is then added to the reverse variable after multiplying the past value of the reverse variable by 10.
- At last, the input number n is divided by 10. This statement acts as the decrement statement for the while loop.
- Finally, we check whether the reversed number is equal to the original input number, i.e., check if temp == reverse is True or not.
Now, Let's look at an example to understand the while loop method:
Output:
To perfectly grasp the working of the above example, let's notice the value of every variable after each iteration of the while loop:
Initial Values:
- n = 123
- reverse = 0
- temp = 123
First Iteration:
- digit = (n % 10) = 3
- reverse = (reverse * 10 + digit) = 3
- n = (n / 10) = 12
Second Iteration:
- digit = (n % 10) = 2
- reverse = (reverse * 10 + digit) = 32
- n = (n / 10) = 1
Third Iteration:
- digit = (n % 10) = 1
- reverse = (reverse * 10 + digit) = 321
- n = (n / 10) = 0
The while loop will terminate after the Third iteration, as the condition, i.e., n>0 results in False. Hence, by using the modulus operation (%) and while loops, we can check the palindrome in Python.
Note - In this method, we are copying the input number to a temp variable to make it available for comparison later in the program. Hence, this method requires extra space.
Highlights:
(1) Naive approach
(2) Uses mathematical formula and iteration to reverse the number.
(3) It Needs extra space to copy the input number to compare it with the reversed one.
Python Program to Check If a Number is Palindrome or Not Using Recursion
Another way to check palindrome in Python is by converting the input number to a sequence (String or List) and then using the length technique, i.e., applying a manual reverse on top of the length of the sequence.
In this method, we check whether the first element of the sequence is the same as the last element, the second element is the same as the second last one, and so on. If any element mismatches, the number is not a palindrome. This method can be implemented using recursion.
Let's look at the recursion implementation of the Length Technique:
Output:
Here, when the function check_palindrome is called, the following steps take place:
- Firstly, we convert the input number n to a string using the built-in str() function.
- Then, we find out the length of the string using the built-in len() function to verify if the string is empty or just a single character long.
- Finally, the recursive case is executed in which:
- We check if the first character of the string (s[0]) is the same as the last character (s[len(s)-1]) or not. If it doesn't match, the program halts, and the function returns the value False. But, if it matches, then these two characters are removed from the string via slicing (s[1 : len(s) - 1]), and the new string is sent as an argument to the recursive function.
- For a palindrome number, this process is continued until we are left with the string of unit (input number has odd digits) or zero-length (input number has odd digits), i.e., the base case is attained.
- Hence, in every function call, we are reducing the sequence size by 2.
Highlights
(1) Number is converted to a sequence and passed to a recursive function.
(2) Implements Manual Length Technique.
(3) At each iteration, the function reduces the sequence by 2.
Python Program to Check If a Number is Palindrome or Not Using Built-in Functions
We can also check palindrome in Python with the help of the built-in reversed() function provided for sequence data types in Python:
- reversed(sequence): It returns an iterator that accesses the sequence passed as an argument in the reverse order.
To check palindrome in Python using the built-in reversed(sequence) function, we can convert the input number to a sequence and find the reverse of the sequence using the reversed method. After which, we can compare these two sequences to check the palindrome.
Let's look at an example to understand the concept of the reversed() function:
Output:
Here, we are converting the input number n to a string using the str() function and storing it to a variable seq. Then, we check whether the result of the reversed(seq) function is equal to that of the string seq. If it matches, the input number n is a palindrome.
Highlights: (1) Number is converted to a sequence.
(2) Reverses the number using the built-in reversed(sequence) function.
Palindrome in Python Using Slice Operator
Another way in which we can make use of sequences to check palindrome in Python is by using the slice operator:
- Slice operator [n : m : step-size] - It returns the part of the sequence starting from the nth element to the (m-1)th element by iterating the sequence according to the defined step size.
After converting the input number to a sequence, we can generate a reversed sequence by using a negative integer as the step-size argument. This reversed sequence is compared with the sequence generated from the input number to check the palindrome. It is the simplest way to check the palindrome of a number in Python.
Let's look at an example to understand the use of slicing to check palindrome:
Output:
Here, s[::-1] indicates that the step size is -1, and since no other arguments are given, it iterates the whole list in reverse order.
Note- For a large number, the built-in function reversed(sequence) performs better than the slice operator. This is because the reversed(sequence) method returns an iterator that accesses the sequence in reverse order. At the same time, the slice operator creates a new list that contains the elements in reverse order as that of the original list.
Highlights:
(1) Number is converted to a sequence and sliced with a negative step-size.
(2) Simple method.
(3) Slower than reversed(sequence) function for large numbers.
Conclusion
- To check a number's palindrome, you must find its reverse first.
- We can check palindrome in Python using: Iterative, Recursion, Built-in functions, and Slicing of sequence.
- In the iterative method, we reverse the number using a mathematical formula and while loops.
- In the recursion method, we implement the manual length technique, i.e., we check whether the first digit is equal to the last, the second is equal to the second last, and so on.
- We can use the built-in reversed(sequence) function to reverse the number by converting it into a sequence and applying the function.
- We can also use the Slice operator having a negative step size to reverse the number and check the palindrome.