Check for Palindrome Using Python

A sequence (string/number/phrase) is said to be a palindrome if the sequence reads the same backward as forward. For example, madam, civic, refer, 14541, and level.

This article discusses four methods of checking if a sequence object is a palindrome in Python. These methods include

  • Method 1: Using object indexing and slicing,
  • Method 2: Using an iterative loop (for-loop or while loop),
  • Method 3: Using the inbuilt reversed() function,
  • Method 4: Using a recursive approach (you may find this in a coding interview).

In the last section, we will also cover some potential caveats when using those methods to check for palindrome.

Method 1: Using object indexing and slicing

The general syntax for object slicing is given by object[start:stop:step], and the syntax object[::-1] is used to reverse the sequence.

Here are the reasons why that works:

  • If start and stop are not supplied, the sequence is sliced from the beginning to the end. This is what we want to do.
  • Negative values are used for indexing from the end of the sequence.

The following is an example of using reverse indexing to check for palindrome in Python.

Output:

refer True
python False

Method 2: Using an iterative loop (for-loop or while loop)

Run a loop from the start to the midpoint of the string and check the first character to the last character, the second to the second last, and so on. If any character mismatches, the string is not a palindrome.

The following figure shows how a loop to identify palindrome works.

Output:

python False
saippuakivikauppias True

Output:

True
False
True

Method 3: Using reversed() function

The reversed(<seq>) function returns a generator of sequence objects in reversed order.

Output:

is a palindrome.

Method 4: Using a recursive function

This method can show up in a coding interview in the form of a question like this:

“Write a code to check if a string is a palindrome using a recursive approach.”

Output:

python False
saippuakivikauppias True

The following figure shows how the recursive function explained above works.

Bonus: Checking for palindromes in a list

Output:

Orginal list of strings:
 ['refer', 'dad', 'python', 'malayalam', 'rotary', 'aba']
Palindromes:
 ['refer', 'dad', 'malayalam', 'aba']

Wrapping Up – 3 essential points

There are at least three things that affect whether Python correctly identifies a palindrome. They are

  • Data type – the methods discussed above require the input to be a string type.
  • Whether the string is lowercase or uppercase – Python is case-sensitive. For that reason, Mom, Refer, and Redivider will not be identified as palindromes because the first characters are upper case. To solve this potential problem, convert the input into lowercase or uppercase before checking if a given string is a palindrome.
  • Punctuations also affect if a word is identified as a palindrome or not. For example, without punctuation marks, the following phrase is a palindrome:

“A man, a plan, a canal: Panama”.

A simple way to solve the problem of punctuation is to remove them. Here is how to do that:

Output:

A man a plan a canal Panama
AmanaplanacanalPanama
amanaplanacanalpanama

The following code shows how to incorporate those three points into the solution discussed in method 1.

Output:

Refer True
A man, a plan, a canal: Panama True
12321 True

Conclusion

This article covered four ways of checking if a Python sequence is a palindrome. The first method should suffice in most cases, but in some cases (like in coding interviews), you might be required to use one of the other methods discussed in the article.

You also need to go through the three points mentioned in the last section. They provide solutions to caveats you might face when checking for palindrome.