Please click on ads

Please click on ads

Tuesday, 4 July 2023

write a python program for palindrome

Certainly! Here's a Python program to check if a given word or phrase is a palindrome: ```python def is_palindrome(word): # Remove any whitespace and convert to lowercase word = word.replace(" ", "").lower() # Check if the word is equal to its reverse if word == word[::-1]: return True else: return False # Prompt the user to enter a word or phrase word = input("Enter a word or phrase: ") # Check if the input is a palindrome if is_palindrome(word): print("Yes, it is a palindrome.") else: print("No, it is not a palindrome.") ``` In this program, we define a function `is_palindrome` that takes a word as input. It removes any whitespace and converts the word to lowercase. Then, it checks if the word is equal to its reverse (using slicing with `[::-1]`). If the word and its reverse are equal, the function returns `True`, indicating that it is a palindrome. Otherwise, it returns `False`. We prompt the user to enter a word or phrase and then call the `is_palindrome` function to check if it is a palindrome. Finally, we display the appropriate message based on the result.

No comments:

Post a Comment