Palindrome in Python

Feature Img Palindrome No

Today we are going to learn about the palindrome series and how to implement and identify a palindrome in Python. So let’s dive right into it!

What is a Palindrome?

A number is defined as a Palindrome number if it reads the exact same from both forward and backward. And the crazy thing is that it is not only valid to numbers. Even if a string reads the same forwards and backward, then it is a Palindrome as well!

Let us look at some examples to understand it better.

What is a Palindrome series?

1. Palindrome Numbers

Let us consider two numbers: 123321 and 1234561.

The first number 123321, when read forward and backward is the same number. Hence it is a palindrome number.

On the other hand, 1234561, when reading backward is 1654321 which is definitely not the same as the original number. Hence, it is not a Palindrome Number.

2. Palindrome Strings

The logic that was explained for the Palindrome Numbers is also applicable to the strings. Let’s consider two basic strings: aba and abc.

String aba reads the same no matter how it is read (backward or forward). But on the other hand string abc when reading backward results in cba which is not same as the original string.

Hence aba is a Palindrome while abc isn’t.

How to verify for Palindrome?

1. Palindrome Numbers

To check if a number is a Palindrome number or not, we first take the input of the number and create a copy of the number taken as an input.

We then create a new variable to store the reversed number and initialize it with 0.

Traverse through the number using mod 10 and division by 10 operations and in each loop make sure to add the digit in the reversed number variable*10.

2. Palindrome Strings

To check for a string, we take a string as input and calculate its length. We also initialize an empty string to store the reverse of the string.

We create a decrementing loop starting from the last index and going to the first and each time concatenate the current reversed string with the new letter obtained.

Pseudo-code to implement Palindrome in Python

1. Palindrome Numbers

READ n
CREATE A COPY OF n as c_n
CREATE r_v = 0 ( to store reversed number)
WHILE n!=0:
d=n%10
r_v=r_v*10+d
n=n/10
if(c_n == r_v):
print "PALINDROME"
else:
print "NOT PALINDROME"

2. Palindrome Strings

READ s
CALCULATE length of s l_s
CREATE r_s = "" ( to store reversed string)
FOR i: l_s-1 -> 0
r_s + = s[i]

if(r_s == s):
PRINT "PALINDROME"
else:
PRINT "NOT PALINDROME"

Code to implement Palindrome Checking in Python

Now that you know what Palindromes are and how to deal with them in the case of strings and numbers, let me show you the code for both.

1. Palindrome Implementation: Numbers

Let’s check for palindrome numbers using Python.

n = input()
n = int(n)
copy_n=n
result = 0

while(n!=0):
    digit = n%10
    result = result*10 + digit
    n=int(n/10)

print("Result is: ", result)
if(result==copy_n):
    print("Palindrome!")
else:
    print("Not a Palindrome!")

2. Palindrome Implementation: Strings

Let’s now check for Palindrome strings in Python

s = input()
l_s=len(s)
r_s=""

for i in range(l_s-1,-1,-1):
    r_s+=s[i]

print("Reuslt is: ",r_s)
if(r_s==s):
    print("PALINDROME")
else:
    print("NOT PALINDROME")

Palindrome Numbers

123321
Result is:  123321
Palindrome!

Palindrome Strings

aibohphobia
Reuslt is:  aibohphobia
PALINDROME

Conclusion

Congratulations! Today in this tutorial you learned about Palindromes and how to implement them as well! Hope you learned something! Thank you for reading!