Palindrome Check of a given word or number using python3?
In this Python tutorial, we will learn how to check if a number or word is palindrome or not using Python. With this tutorial, you will also learn how to check palindrome ignoring the case of characters in a string in Python.
PALINDROME CHECK in Python
Palindrome Check: The process to check whether a given word or number is equal to it’s reverse or not, e.g., abcba, 96569 etc. It requires to reverse the number or word first and then check whether it is equal or not.
In python programming, it’s not too difficult to find the reverse of a word or number. The following is the code snippet and the outputs written in Python3.
PROGRAM: Check if a word or number is palindrome or not in Python
item=str(input("Input a word or number: ")) #input in string format reverse="".join(reversed(item)) #reversal of the input" #check whether the input is equal to it's reverse or not if(item==reverse): print("YES, the entered item is palindrome.",end='') else: print("NO, the entered item is not a palindrome.",end='')
Input a word or number: POPOP YES, the entered item is palindrome.
Input a word or number: 7894 NO, the entered item is not a palindrome.
Input a word or number: 54645 YES, the entered item is palindrome.
Python program to check if a string is a palindrome ignoring the case of characters
Now suppose you got a string like this, Madam
If we reverse this string we will get madaM. But as this is not similar to the first string, Madam – It will be considered as a non-palindrome number. But we want to make it considered as a palindrome. So what will we do?
We will simply convert all the characters to either upper case or lower case then we will reverse it. After that, we will compare the reversed string to the new converted string.
To make it easy to understand I will suggest you read, Uppercase Lowercase Conversion of an Input String in Python
To get the full algorithm, Check If A String Is Palindrome Or Not Ignoring The Case Of Characters
- Clockwise & CounterClockwise Rotation of Matrix using Numpy in Python3
- How to take only a single character as an input in Python