Hacking Course: Explore a vast array of courses spanning different subjects, from technology and arts to business and personal development. Our expert instructors curate each course to ensure you receive top-notch, up-to-date content.
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.
Subscribe to:
Post Comments (Atom)
-
https://drive.google.com/drive/u/0/my-drive
-
click here
No comments:
Post a Comment