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 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 into ‘Not Palindrome’.
Therefore we will first use inbuilt lower()
to convert the string into the lowercase, then we will reverse the string and will compare it with the original string to check if its palindrome or not.
Let’s see the python source code.
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
If you have any suggestions or doubts then please comment below. Also, do like our FB page for interesting stuff.