Python program to check if number is palindrome (one-liner)
Last Updated :
11 Jul, 2025
In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article.
Input1: test_number = 12321
Output1: True
Input2: test_number = 1234
Output2: False
Palindrome Number Program in Python
Below are the following ways by which we can check if the number is palindrome or not in Python in one line:
Palindrome Program using math.log() + recursion + list comprehension
In this example, we are using math.log(), recursion, and list comprehension to check if the number is palindrome or not. Here, the logs function extracts the number of digits which is powered by 10 to get the number for that iteration for comparison and then recursion test for palindrome.
Python3
import math
def rev(num):
return int(num != 0) and ((num % 10) * \
(10**int(math.log(num, 10))) + \
rev(num // 10))
test_number = 9669669
print ("The original number is : " + str(test_number))
res = test_number == rev(test_number)
print ("Is the number palindrome ? : " + str(res))
OutputThe original number is : 9669669
Is the number palindrome ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Python Check Palindrome Using str() + string slicing
In this example, we are converting the number into a string and then reversing it using the string slicing method and comparing it whether the number is palindrome or not
Python3
# initializing number
test_number = 9669669
print ("The original number is : " + str(test_number))
# using str() + string slicing
# for checking a number is palindrome
res = str(test_number) == str(test_number)[::-1]
print ("Is the number palindrome ? : " + str(res))
OutputThe original number is : 9669669
Is the number palindrome ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Python Palindrome Number using user input + string slicing
In this example, we are taking user input in string and then check if the number is palindrome or not.
Python3
num = input("Enter a number")
if num == num[::-1]:
print("Yes its a palindrome")
else:
print("No, its not a palindrome")
Time Complexity: O(n)
Auxiliary Space: O(1)
Palindrome Program in Python Using all() and zip()
In this example, we are using a generator expression with the zip() function and the all() function to check whether the number is palindrome or not. The generator expression (a == b for a, b in zip(str(12321), reversed(str(12321)))) generates a generator that returns True if the elements a and b are equal, and False otherwise. The all function returns True if all of the elements in the generator are True, and False otherwise. In this case, it would return True because all of the elements in the generator are True.
Python3
# Using the all function and a generator
# expression to check if a number is a palindrome
print(all(a == b for a, b in zip(str(12321),
reversed(str(12321)))))
# prints True
print(all(a == b for a, b in zip(str(1234),
reversed(str(1234)))))
# prints False
Time complexity: O(n), where n is the length of the number.
Space complexity: O(1)
Similar Reads
Python Program to Check if a String is Palindrome or Not The task of checking if a string is a palindrome in Python involves determining whether a string reads the same forward as it does backward. For example, the string "madam" is a palindrome because it is identical when reversed, whereas "hello" is not. Using two pointer techniqueThis approach involve
3 min read
Python Program To Check If A Singly Linked List Is Palindrome Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol
6 min read
Python Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco
2 min read
Python Program to find all Palindromic Bitlists in length In this article, we will learn to generate all Palindromic Bitlists of a given length using Backtracking in Python. Backtracking can be defined as a general algorithmic technique that considers searching every possible combination in order to solve a computational problem. Whereas, Bitlists are list
6 min read
Python program to check whether number formed by combining all elements of the array is palindrome Given an array arr[], the task is to combine all the elements in the array sequentially and check if it is a palindrome. Examples: Input: arr[] ={1 , 69 , 54 , 45 , 96 , 1} Output: palindrome Explanation: The number formed by combining all the elements is "1695445961" which is a palindrome Input: ar
4 min read
Python Program To Check String Is Palindrome Using Stack A palindrome is a sequence of characters that reads the same backward as forward. Checking whether a given string is a palindrome is a common programming task. In this article, we will explore how to implement a Python program to check if a string is a palindrome using a stack. Example Input: str =
3 min read