Python programs to check Palindrome strings and numbers
The Palindrome is a number, string or sequence that remains same when it reverse. A palindrome sequence can be a combination of digits or numbers, but it looks same in forward and backward. It is generally used where recreational mathematics is applied, like - puzzles, games. To check whether a number or string is palindrome or not, simply reverse it and compare it with original number or string. If both are same then the number or string is a palindrome otherwise not.
These are the some examples of Palindrome numbers and strings -
121, 1991, 1239321, MAAM, DAD, WOW
When we reverse the above numbers or strings this will remain the same. So, all the above are palindrome.
Check Palindrome numbers using While loop
In the given code, we have some numbers in the list. We have used the while loop and reverse each integer and compare it with original number to check whether it is a palindrome or not.
data = [121, 1991, 1239, 343343, 28981]
for x in data:
temp = int(x)
rev = int()
while(temp > 0):
d = temp % 10
rev = (rev * 10) + int(d)
temp = temp//10
if(rev == x):
print("{} is a Palindrome".format(x))
else:
print("{} is not a Palindrome".format(x))
The above code returns following output -
121 is a Palindrome
1991 is a Palindrome
1239 is not a Palindrome
343343 is a Palindrome
28981 is not a Palindrome
Check Palindrome numbers in a given range
Here, we have checked the Palindrome numbers up to a given maximum value.
max = int(input("Enter the maximum number : "))
print("Palindrome Numbers between 1 and %d are : " %max)
for x in range(1, max + 1):
temp = int(x)
rev = int()
while(temp > 0):
d = temp % 10
rev = (rev * 10) + int(d)
temp = temp//10
if(rev == x):
print("{} is a Palindrome".format(x))
The above code returns output something like this -
Enter the maximum number : 50
Palindrome Numbers between 1 and 50 are :
1 is a Palindrome
2 is a Palindrome
3 is a Palindrome
4 is a Palindrome
5 is a Palindrome
6 is a Palindrome
7 is a Palindrome
8 is a Palindrome
9 is a Palindrome
11 is a Palindrome
22 is a Palindrome
33 is a Palindrome
44 is a Palindrome
Check Palindrome strings using slice operator
The slice operator is in the format [n:m], which returns a slice of the string from the nth character to the mth character. The first index is optional. If we omit the first index, i.e., before the colon, the slice starts at the beginning of the string. The [: m] will slice list from starting till mth index that is why [: : -1] prints list in reverse order. The negative value shows right to left traversal.
Here is the program to check palindrome strings using the slicing operator. Simply, we first reverse each string using slice operator and compare it with the original string.
data = ['MAAM', 'SUPER', 'DAD', 'WOW', 'HAPPY']
for x in data:
if(x == x[::-1]):
print("{} is a palindrome".format(x))
else:
print("{} is not a palindrome".format(x))
The above code returns the following output -
MAAM is a palindrome
SUPER is not a palindrome
DAD is a palindrome
WOW is a palindrome
HAPPY is not a palindrome
Related Articles
Python convert list to numpy arrayPython Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml