Python Program to Check Palindrome Number
You can check a palindrome number in Python by converting the number to a string and comparing it with its reverse using
str(num) == str(num)[::-1].Examples
Input121
Output121 is a palindrome number
Input123
Output123 is not a palindrome number
Input0
Output0 is a palindrome number
How to Think About It
To check if a number is a palindrome, think of it as a word that reads the same forwards and backwards. Convert the number to a string, then compare the string with its reversed version. If both are the same, the number is a palindrome.
Algorithm
1
Get the input number.2
Convert the number to a string.3
Reverse the string.4
Compare the original string with the reversed string.5
If they are equal, return that the number is a palindrome.6
Otherwise, return that it is not a palindrome.Code
python
num = int(input('Enter a number: ')) num_str = str(num) if num_str == num_str[::-1]: print(f'{num} is a palindrome number') else: print(f'{num} is not a palindrome number')
Output
Enter a number: 121
121 is a palindrome number
Dry Run
Let's trace the number 121 through the code
1
Input number
num = 121
2
Convert to string
num_str = '121'
3
Reverse string
num_str[::-1] = '121'
4
Compare strings
'121' == '121' is True
5
Print result
Output: '121 is a palindrome number'
| num_str | num_str[::-1] | Comparison Result |
|---|---|---|
| 121 | 121 | True |
Why This Works
Step 1: Convert number to string
We use str(num) to treat the number like text so we can reverse it easily.
Step 2: Reverse the string
Using num_str[::-1] creates a reversed copy of the string.
Step 3: Compare original and reversed
If both strings are the same, the number reads the same forwards and backwards, so it is a palindrome.
Alternative Approaches
Using arithmetic to reverse number
python
num = int(input('Enter a number: ')) original = num reverse = 0 while num > 0: digit = num % 10 reverse = reverse * 10 + digit num //= 10 if original == reverse: print(f'{original} is a palindrome number') else: print(f'{original} is not a palindrome number')
This method uses math to reverse the number without converting to string, which is useful if you want to avoid string operations.
Using recursion to check palindrome
python
def is_palindrome(s): if len(s) <= 1: return True if s[0] != s[-1]: return False return is_palindrome(s[1:-1]) num = input('Enter a number: ') if is_palindrome(num): print(f'{num} is a palindrome number') else: print(f'{num} is not a palindrome number')
This method uses recursion to check palindrome by comparing first and last characters repeatedly.
Complexity: O(n) time, O(n) space
Time Complexity
The time depends on the number of digits n because reversing the string and comparing takes O(n) time.
Space Complexity
Converting the number to a string and creating its reverse uses O(n) extra space.
Which Approach is Fastest?
The arithmetic method uses O(1) space and is efficient, but string method is simpler and more readable for beginners.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String reversal | O(n) | O(n) | Simplicity and readability |
| Arithmetic reversal | O(n) | O(1) | Memory efficiency and no string use |
| Recursive check | O(n) | O(n) | Learning recursion and string manipulation |
Convert the number to a string and compare it with its reverse to quickly check for palindrome.
Forgetting to convert the number to a string before reversing causes errors or wrong results.