Nuances of identifying Palindrome with Python Strings

1872

One of the introductory examples in the world of programming the algorithm to identify Palindrome strings. Being the most widely used language, python is no exception to this fact. However, like any programming language, python has its share of nuances. For instance, it has its way of dealing with data types and data structures. One such data structure is a string. Yes, the string is a data structure in Python as opposed to being a primitive datatype in some of the languages. In this article, we will walk through a few nuances of a string through the palindrome program.

Also Read: Log Analytics with Python Pandas Explode

What is Palindrome?

It is simple; any string which is equal to it’s reversed string is a palindrome string. Examples of this include the word ‘MALAYALAM’ or ‘GADAG’ or ‘MADAM’ etc. Hence, from an algorithmic standpoint, it must suffice if we compare the string with the reverse of a string. However, strings have an important property in python:

Strings in Python are immutable.

Let’s take an example. We will use the function replace to demonstrate the immutability of a string. Before that let’s declare a string.

S1 = "Good Morning Data4v"

In S1, let’s replace the word ‘Morning’ by ‘Afternoon’ and assign it to S2.

S2 = S1.replace("Morning", "Afternoon")

However, when we print S1 and S2, it is evident that the replaced string creates a new object altogether.

This immutability of strings in python makes things interesting in palindrome algorithm of python. Nonetheless, there are two ways to implement palindrome in python:

1. Reverse slicing of strings

This is a pretty straightforward method. Let’s consider a string named MALAYALAM for example.

str1 = 'MALAYALAM'

Next, we will reverse the string using indexing.

str2 = str1[::-1]

Finally, we use the if-else condition as follows.

if (str1==str2) :
print('The string is palindrome')
else :
print('Not Palindrome')

2. Using reversed()

Now, with the function reversed, things get interesting. The reversed() function helps us reverse a sequence. However, it does not return a reversed string but an iterator(or a pointer) to the end of the string to be reversed. Let us try to understand this with a code snippet.

str1 = 'MALAYALAM'

Let us use the function reversed().

str2 = reversed(str1)

Finally, let us check for the palindrome with an if-else condition.

if (str1==str2) :
print('The string is palindrome')
else :
print('Not Palindrome')

You might be intrigued why this does not work. The reason is as mentioned before. The reversed function does not reverse the string but gives us an iterator. Let’s see this with code.

We can deal with this in two ways:

> Use the list() constructor to create two lists from the original string and the reversed iterator to the string. Further, compare the two lists.

if (list(str1)==list(str2)) :
print('The string is palindrome')
else :
print('Not Palindrome')

> Use join() method to create a new string using the reversed iterator.

str1 = 'MALAYALAM'

str2 = ''.join(reversed(str1))

if (str1==str2) :
print('The string is palindrome')
else :
print('Not Palindrome')

Conclusion

Hope that this article helps you. However, we encourage to take it forward and play around with the code.



I am a Data Scientist with 6+ years of experience.


Leave a Reply