Problem: Write a program in python to check whether a string is a palindrome string or not.
Example:
Input: mom Output: Palindrome Input: Malayalam Output: Palindrome Input: Pencil Output: Not Palindrome
To check palindrome string in Python, we need to follow the following steps:
- Input a string (
str
). - Reverse the string (
rev
). - Check
if str == rev
. - If YES, then the string is palindrome else not.
Before reversing the string we have to be sure that all the letters in the given string are in the same case (lowercase or uppercase) because if not then Mom would result in ‘Not Palindrome’.
Therefore we use the built-in lower()
function to first convert the string into the lowercase then reverse it and finally compare it with the original string.
Here is the implementation of the steps in Python:
str = input("Enter a string: ")
#convert string to lowercase
str = str.lower()
#reverse the string
rev = str[::-1]
if rev == str:
print("Palindrome")
else:
print("Not Palindrome")
Output 1:
Enter a string: mom Palindrome
Output 2:
Enter a string: pencil Not Palindrome