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 |
|---|
| Python Strings |
| Exercises & Assignments |
|---|
| Python program to check palindrome string |
| Python program to count vowels in a string |
| Python program to reverse a string |
| Interview Questions & Answers |
|---|
| How would you convert a string into lowercase? |
| How would you convert a string into uppercase? |
| Why do we use the join() function in Python? |
| What are f-strings in Python? |