A number that remains the same even if its digits are reversed is called a Palindromic Number. Examples are 99, 1001, 14641, 32123, etc.
There are four ways to check if a given number is a palindrome in Python. This article will explain each approach with examples.
1. How to check Palindrome Number in Python
The simple and best way is string slicing. String slicing in Python extracts a substring or entire string. It creates a new string containing the selected characters without modifying the original.
Below are the steps to check palindrome number using string reversal method:
- Convert number to string
First. convert the given number to string using the
str()function.
For Example,str(12321) = "12321" - Reversing the String
Next, use the Python’s slicing technique
[::-1], to create a reversed copy of the string. For Example,'12321'[::-1] = "12321" - Compare the original string with its reverse
Now, compare the original string with the reversed string. If both are the same, the number is a palindrome; otherwise, it is not. For Example,
"12321" == "12321"returnsTrue.
Code Example
Output:
12321 is a palindrome.
2. Mathematical Reversal (Without String Conversion)
In this approach instead of converting the number into a string, we reverse the number mathematically and compare it with the original string. Let’s see demonstrate this with an example.
We need to use while loop, modulo operator to achieve this.
Code Example
Explanation
1. Initialize the variables
numberholds the value you want to check (in this case, 12321).original_numberis a copy of the number so you can compare it later after reversing it.reversed_numberstarts at 0 and will be used to build the reversed version of the number.
2. Use while loop to reverse the digits of the number
- Extract the last digit using the modulus operation (
digit = number % 10). For example, if the number is 12321, this operation gives 1 (the last digit). - Append the digit to
reversed_number(reversed_number = reversed_number * 10 + digit):- Initially,
reversed_numberis 0, so when the first digit (1) is added,reversed_numberbecomes 1. - Each time a new digit is extracted from a number, it is appended to
reversed_numberby shifting the current digits to the left (multiplying by 10) and adding the new digit. - For example,
after the first iteration,reversed_number = 1;
after the second iteration,reversed_number = 12;
and after all iterations,reversed_number = 12321. - Remove the last digit from the number by dividing it by 10 (
number //= 10). For example, if a number is 12321, after this operation, it becomes 1232.
- Initially,
3. After the loop, you compare the reversed_number to original_number. If they are the same, the number is a palindrome.
3. Recursive Method
Recursion is a programming technique where a function calls itself to solve smaller instances of a problem until a base condition is met. In Python, a recursive method can also be used to check if a number is a palindrome by repeatedly comparing the first and last digits.
Code Example
Explanation
- Base Case: If
num == 0This means we have processed all digits and can return therevcontaining the reversed number. - Recursive Case: Each recursive call extracts the last digit of
numand adds it torevafter shiftingrevby multiplying it by 10. - Final Check: After recursion completes, you compare the original number with the reversed version stored in
revreturned byis_palindrome_recursive()function.
Let’s work out one example step by step in detail for number = 12321
- First Call: is_palindrome_recursive(12321, 0)
- rev = (0 * 10) + (12321 % 10) = 1
- Calls is_palindrome_recursive(1232, 1)
- Second Call: is_palindrome_recursive(1232, 1)
- rev = (1 * 10) + (1232 % 10) = 12
- Calls is_palindrome_recursive(123, 12)
- Third Call: is_palindrome_recursive(123, 12)
- rev = (12 * 10) + (123 % 10) = 123
- Calls is_palindrome_recursive(12, 123)
- Fourth Call: is_palindrome_recursive(12, 123)
- rev = (123 * 10) + (12 % 10) = 1232
- Calls is_palindrome_recursive(1, 1232)
- Fifth Call: is_palindrome_recursive(1, 1232)
- rev = (1232 * 10) + (1 % 10) = 12321
- Calls is_palindrome_recursive(0, 12321)
- Base Case: is_palindrome_recursive(0, 12321)
- Since num == 0, the function returns rev = 12321.
- Final Check:
- After recursion, rev_num = 12321; since number == rev_num (12321 == 12321), the number is confirmed to be a palindrome.
4. Using Stack (Last In, First Out)
Below is the Python example for checking if the number is palindrome or not using stack. This method involves using a stack to reverse the digits of the number and then comparing.
In this code
- We converted the number into a string.
- Stack Creation: Push all characters onto a stack (which follows Last In, First Out).
- Reversing: Pop each character from the stack using
stack.pop()function and add it to the reversed string. To create the reversed string. - Comparison: Compare the original string with the reversed one.

Leave a Reply