Python Code for Checking a Palindrome

A palindrome is a sequence that remains unchanged when reversed, such as 121, madam, or level. To determine whether a string or number is a palindrome, take input from the user, reverse it, and compare the reversed sequence to the original. If both are the same, it is a palindrome; otherwise, it is not.

Here is a Python program to check if a given input is a palindrome:

import sys

def is_palindrome(value):
    # Reverse the string and compare with the original
    return value == value[::-1]

def main():
    """
    palindrome checker in Python
    """
    # provide a value as command-line argument
    args = sys.argv
    # check if user provided a value or not
    if len(args) < 2:
        print("no input value provided")
    else:
        inp_val = str(args[1])
        # Check for palindrome and display the result
        if is_palindrome(inp_val):
            print(f"'{inp_val}' is a palindrome")
        else:
            print(f"'{inp_val}' is not a palindrome")


if __name__ == "__main__":
    main()

Save the above code as check_palindrome.py (or any name you like) and run it as follows:

python check_palindrome.py 123454321

Replace 123454321 with your desired input and the code will return whether your input is a palindrome or not.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.