Home Tutorials Python Programs Palindrome
Palindrome

Palindrome


Topic: Palindrome String Checker

Description: This program takes a string input from a user, cleans it by removing spaces and punctuation, and evaluates whether it reads the same backward as forward (a palindrome).

Why: Palindrome checking introduces students to advanced **string slicing tricks**, boolean evaluation logic, and string filtering methods using loop comprehensions to normalize data parameters before processing.


Program Code

def is_palindrome(text):
    # Normalize string: lower case and keep only alphanumeric characters
    cleaned_text = "".join(char.lower() for char in text if char.isalnum())
    
    # Compare the cleaned text against its exact reverse string sequence
    return cleaned_text == cleaned_text[::-1]

def run_palindrome_system():
    print("=============================")
    print("  PALINDROME TEXT DIAGNOSTIC ")
    print("=============================")
    
    user_input = input("Enter a word or sentence: ").strip()
    
    if not user_input:
        print("\nError: Input text cannot be completely empty.")
        return

    print("\n-----------------------------")
    if is_palindrome(user_input):
        print(f"Result  : '{user_input}' IS a Palindrome!")
        print("Message : It reads exactly the same forward and backward.")
    else:
        print(f"Result  : '{user_input}' IS NOT a Palindrome.")
    print("-----------------------------")

# Start the routine
run_palindrome_system()

Expected Console Output Examples

=============================
  PALINDROME TEXT DIAGNOSTIC 
=============================
Enter a word or sentence: A man, a plan, a canal: Panama

-----------------------------
Result  : 'A man, a plan, a canal: Panama' IS a Palindrome!
Message : It reads exactly the same forward and backward.
-----------------------------

Core Syntax & Concept Breakdown

  • Advanced Slicing ([::-1]): Passing a negative step parameter value of -1 inside a slice instruction tells Python to traverse a string completely from back to front, making structural reversal basic.
  • Inline Join Generators: "".join(...) takes individual filtered characters generated inside a loop comprehension sequence and binds them cleanly back together into an optimized scalar string string.

🏋️ Test Yourself With Exercises

Take our quiz on Palindrome to test your knowledge.

Browse Quizzes »