Palindrome Number in Python
Introduction
This blog will go over how to determine whether a number is a Palindrome without taking up additional space.
Accordingly, let's first understand a palindrome before moving on to the problem. Our today’s discussion will be based on the Palindrome number in Python.

What is Palindrome?
A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward. In other words, it is a sequence of characters that is spelled the same way from left to right and right to left. Examples of palindromes include "racecar," "level," and "madam." Palindromes are often used in puzzles, word games, and cryptography.
What is a Palindrome Number?
A number that is unchanged when the number or digits are reversed is known as a palindrome number in Python. For example, the palindrome numbers are 131, 22, 515, 2332, and 84848. I hope now you have got an fair understanding of the Palindrome Number in Python.

Let's have a look at the Problem statement based on Palindrome number in Python.
Problem statement
A number is provided to us. Our objective is to determine whether the provided number is a palindrome or not.
Before we move to the code logic for finding Palindrome Number in Python, let’s understand through some examples of Palindrome numbers in Python.
Sample Examples
The below are some examples of Palindrome Number in Python .
Input: n = 1991991
Output: YES
Explanation:
Original: 1991991
After reversing: 1991991

The given number is the same as the reversed number formed by doing certain operations. This implies that the provided number is a palindrome number in python.
Input: 33568
Output: NO
Explanation:
Original: 33543
After reversing: 86533

The given number is not the same as the reversed number formed. This implies that the number is definitely not a palindrome number in python.
Now, Let’s move on to the code the logic for finding a Palindrome Number using Python.
Palindrome in Python Code
Using While Loop (number)
num = 1001
temp = num
reverse_num = 0
while temp != 0:
digit = temp % 10
reverse_num = (reverse_num * 10) + digit
temp //= 10
if num == reverse_num:
print(num, "is a palindrome number")
else:
print(num, "is not a palindrome number")
In this code, we first take the input number from the user. We then create a temporary variable to store the number and a variable called reverse_num to store the reversed number. We use a while loop to extract the digits of the number one by one and build the reversed number by multiplying the current reverse_num by 10 and adding the current digit. Once the loop is done, we check if the original number is equal to the reversed number and print an appropriate message.
Output
1001 is a palindrome number
Using While Loop (strings)
string = "palap"
length = len(string)
i = 0
j = length - 1
while i < j:
if string[i] != string[j]:
print(string, "is not a palindrome string")
break
i += 1
j -= 1
else:
print(string, "is a palindrome string")
In this code, we first take the input string from the user and find its length. We then initialize two variables i and j to point to the first and last characters of the string, respectively. We use a while loop to compare the characters at the i and j indices of the string. If they don't match, we print a message saying that the string is not a palindrome and break out of the loop. If the loop completes without breaking, we print a message saying that the string is a palindrome.
Output
palap is a palindrome string
Palindrome Program in Python
Approach 1: Iterative Method
So, assuming input is provided in the string format or Type (Because if the number is greater than 10^18, then we have to store it in a String variable),
Run a loop from starting to length/2 and check the string's first and last characters, as well as the second and third ones, and so on. Any character mismatch would make the string a non-palindrome Number or string. Here the input is provided in string format
def isPalindrome(str):
#Loop from index 0 to mid of the string.
for i in range(0, len(str)//2):
if str[i] != str[len(str)-i-1]:
return False
return True
# Driver code of function
str = "33568"
if(checkPalindrome(str) == True):
print("Yes, it is a palindrome!")
else:
print("No, it is not a palindrome!")
Output: No, it is not a palindrome!
Time Complexity: O(|str|)
As the for loop inside the function isPalindrome is iterating from 0 to mid of the str, in this case, we can say that the iteration is over n/2 digits, and that makes our complexity as O(|str|)
Space Complexity: O(1)
As there is no extra space used by the program, Hence the space complexity is constant.
Moving ahead to check another approach of the same time and space complexity.
Approach 2: Using the Reverse number
Assuming input is a number here, i.e., it is stored in int Format and not as string.
Run a loop from till the number doesn't get equal to Zero. In every iteration, Take the number %10 to get the Last digit, keep forming the reverse in the iteration, and later divide the number by ten so that number gets shorter. Here the input is not provided in string format.
n=1991991
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("Yes, the number is a palindrome!")
else:
print("No, the number isn't a palindrome!")
Output: Yes, the number is a palindrome!
Time Complexity: O(n)
Here n is the number of digits in the provided number.
Space Complexity: O(1)
As there is no extra space used by the program, Hence the space complexity is constant.
I hope now you understand the logic behind the Palindrome numbers program.
Approach 3: Using recursion
def is_palindrome(n):
"""
Returns True if n is a palindrome number, False otherwise.
"""
# Base case: one-digit numbers are always palindromes
if n // 10 == 0:
return True
# Recursive case: compare first and last digits, then check the remaining digits
first_digit = n % 10
last_digit = n // (10 ** (len(str(n)) - 1))
if first_digit != last_digit:
return False
else:
# Remove the first and last digits from the number and check the remaining digits recursively
return is_palindrome((n % (10 ** (len(str(n)) - 1))) // 10)
# Example usage:
print(is_palindrome(12321)) # True
Output: True
Time Complexity: O(n)
Here n is the number of digits in the provided number.
Space Complexity: O(1)
The space complexity of the above code is O(log n), where n is the input number.
This is because the recursive function is_palindrome() is called recursively for each pair of digits at the beginning and end of the input number. The depth of the recursion is equal to the number of digits in the input number, which is O(log n) in terms of the number of digits.
Let’s move on to the FAQs based on our topic of discussion.
You can practice by yourself with the help of online python compiler for better understanding.
Frequently Asked Question
What is palindrome in Python with example?
In Python, a palindrome is a string, number, or sequence of characters that reads the same backward as forward. Here's an example of a palindrome string in Python: s = "racecar".
In this example, the string s is a palindrome because it reads the same backward as forward.
How do you find the palindrome number?
We can reverse its digits and compare the result with the original number. We can use a while loop to extract the last digit of the number, add it to a new number in reverse order, and then divide the original number by 10.
What is palindrome logic in Python?
The logic to check if a string or number is a palindrome in Python involves reversing the string or number and comparing it with the original. For strings, we can use string slicing to reverse the string, and for numbers.
How do you write a palindrome code in Python?
We can use the logic of reversing the string or number and comparing it with the original. For strings, we can use string slicing to reverse the string, and for numbers, we can use the modulo operator to extract the last digit.
Conclusion
In this article, we have extensively discussed the Problem of Finding Palindrome number in Python.
To learn more about Palindrome, see Palindrome number Problem, Palindrome in C, Palindrome count, Longest Palindromic Subsequence.
Do upvote our blogs if you find them helpful and engaging!
Happy Learning, Ninjas!
