This post shows how to check if a string is a palindrome in Python — a string that reads the same forwards and backwards, like "madam" or "racecar".
Check palindrome string in Python:
Explanation:
We take user input, reverse it using Python's slice notation [::-1], and compare the reversed string to the original. If they match, the string is a palindrome.
Python Program to Check Palindrome String
text = input("Enter a string: ")
reversed_text = text[::-1]
if text == reversed_text:
print(f"{text} is a palindrome")
else:
print(f"{text} is not a palindrome")
Output:
Enter a string: madam
madam is a palindrome
Want to try this yourself without installing Python? Run it directly in our Code Runner.
