Python program to check if number is palindrome (one-liner)
Last Updated :
13 Nov, 2023
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 Check If a Number is a Harshad Number Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in
2 min read
Python program to check if given string is vowel Palindrome Given a string (may contain both vowel and consonant letters), remove all consonants, then check if the resulting string is palindrome or not. Examples: Input : abcuhuvmnba Output : YES Explanation : The consonants in the string "abcuhuvmnba" are removed. Now the string becomes "auua". Input : xayzu
5 min read