Palindrome Checker in Python
About the project: Create a Palindrome Checker project in Python. This will be a self-contained script that checks if a word, phrase, or number reads the same forwards and backward.
This program will ask you to enter a string or a number. It will then tell you if it is a palindrome. The script will run in a continuous loop, so you can check multiple inputs without restarting the program.
How to use this Palindrome Checker:
- Run the Python script.
- This script is ready to use. When you run it, you can enter a word, phrase, or number. The program will tell you if it's a palindrome and will continue to run until you type q to quit.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# palindrome_checker.py
def is_palindrome(text):
"""
Checks if a given string is a palindrome.
This function first cleans the text by removing non-alphanumeric
characters and converting it to lowercase to ensure a proper check.
Args:
text (str): The string to check.
Returns:
bool: True if the string is a palindrome, False otherwise.
"""
# Clean the text: remove spaces and punctuation, and convert to lowercase
# This makes the checker more robust for phrases like "A man, a plan, a canal: Panama"
cleaned_text = ''.join(char for char in text if char.isalnum()).lower()
# Compare the cleaned text with its reverse
return cleaned_text == cleaned_text[::-1]
def main():
"""
Main function to run the Palindrome Checker in a continuous loop.
"""
print("--- Python Palindrome Checker ---")
print("Enter a word, phrase, or number to check if it's a palindrome (or 'q' to quit).")
while True:
user_input = input("\nEnter your text: ").strip()
# Check if the user wants to quit
if user_input.lower() == 'q':
print("Exiting Palindrome Checker. Goodbye!")
break
# Check for empty input
if not user_input:
print("Please enter some text to check.")
continue
if is_palindrome(user_input):
print(f"Result: '{user_input}' is a palindrome.")
else:
print(f"Result: '{user_input}' is not a palindrome.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()