Python program to check palindrome string
Description: Write a Python program to check whether a string is a palindrome.
Rules and hints:
- A palindrome reads the same from both sides, like
madam. - Ignore upper and lower case.
Python program to check a palindrome
def is_palindrome(text):
text = text.lower()
return text == text[::-1]
for word in ("madam", "Level", "python"):
if is_palindrome(word):
print(word, "is a palindrome")
else:
print(word, "is not a palindrome")
Output:
Tutorials Class - Output Window
madam is a palindrome
Level is a palindrome
python is not a palindrome
How it works
lower()changes the text to lowercase, so “Level” becomes “level”.text[::-1]is the reversed string.- If the text equals its reverse, it is a palindrome.
Read about slicing in Python Strings.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |