Python Check Palindrome Number: A palindrome number is a number that remains the same when its digits are reversed. In other words, it reads the same forwards and backwards
Example – 11, 131, 222
Note – Leading zeros are not counted in a palindrome. For example, “010” is not considered a palindrome because it reads the same as “10” without leading zeros.
Python Program to check Palindrome Numbers
You can check if a number is a palindrome in Python by reversing the number and comparing it to the original number. If they are the same, the number is a palindrome.
- Convert the number to a string, reverse the string, and compare it to the original string.
- Use mathematical operations to reverse the number and compare it to the original number.
Algorithm
This algorithm works by converting the number to a string, reversing the string, and then comparing it to the original string to determine if it’s a palindrome. If the reversed string is the same as the original string, the number is considered a palindrome
- Define the
is_palindromefunction with a single argumentnum, - Inside the function, convert the number
numto a string using thestr()function. - Reverse the string by using slicing with
[::-1]. - Compare the original string
num_strwith the reversed stringreversed_strto check if they are the same. - If the original string and the reversed string are the same, return
True. Otherwise, returnFalse. - In the main part of the code, prompt the user to input a number using
input(). The input is then converted to an integer usingint()and stored in the variablenum. - Call the
is_palindromefunction with the input numbernum. - If the function returns
True, print a message indicating that the number is a palindrome. Otherwise, print a message indicating that it’s not a palindrome
Source code
In this code, we first convert the number to a string using str(), then use slicing ([::-1]) to reverse the string. Finally, we compare the original string with the reversed string to determine if the number is a palindrome
def is_palindrome(num):
num_str = str(num)
reversed_str = num_str[::-1]
if num_str == reversed_str:
return True
else:
return False
num = int(input("Enter a number: "))
if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")
Steps to write Program
Step-1: Define a function named is_palindrome that takes one parameter, num.
def is_palindrome(num):
Step-2: Convert the input number num into a string and store it in the variable num_str.
num_str = str(num)
Step-3: Create a new string called reversed_str by reversing the characters in num_str.
reversed_str = num_str[::-1]
Step-4: Check if the num_str and reversed_str strings are the same. If they are equal, return True; otherwise, return False.
if num_str == reversed_str:
return True
else:
return FalseStep-5: Input a number from the user and store it in the variable num using the input() function.
num = int(input("Enter a number: "))Step-6: Call the is_palindrome function with the input number num as an argument and check if it returns True or False. If is_palindrome(num) returns True, print that the input number is a palindrome. Otherwise, print that it is not a palindrome.
if is_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")Output
The output of the code depends on the input you provide. Let’s go through a couple of examples to see the possible outputs:
If you input the number 12321, which is a palindrome. The code correctly identifies it as a palindrome and prints the message “12321 is a palindrome.”
Enter a number: 12321 12321 is a palindrome.
you input the number 45678, which is not a palindrome. The code correctly identifies it as not a palindrome and prints the message “45678 is not a palindrome.”
Enter a number: 45678 45678 is not a palindrome.