Python Program to Check Palindrome Number

A palindrome is a number or string that reads the same backward as forward. For example, 121 is a palindrome number because it reads the same from both side.

# Function to check whether a number is a palindrome or not
def palindrome(n):
    # Convert the number to a string
    num_str = str(n)
    
    # Check whether the string is equal to its reverse
    if num_str == num_str[::-1]:
        return True
    else:
        return False

# Example usage
n = 121
if palindrome(n):
    print(n, "is a palindrome number")
else:
    print(n, "is not a palindrome number")

In the program, the palindrome function takes a number n as input and checks whether it is a palindrome or not.

First, the function converts the number to a string using the str function.

Next, it checks whether the string is equal to its reverse string using the slicing operator [::-1] which reverses the string.

Finally, the function returns True if the string is a palindrome, indicating that the number is a palindrome number. If it’s not, the function returns False.

In the example usage, we pass the number 121 to the palindrome function and print a message based on the result.

Leave a comment