How to Check for Palindrome in Python

A set of letters, characters, or words read backward, just as they are forward, is called a palindrome. If we reverse that number or string, it will be exactly alike to the real number or string. The palindrome can also be a number.

Palindrome strings contain an assortment of characters and some words that remain exactly alike when we read them backward. These collections are also known as symmetrical. If we reverse the given string, it will be the same as the real string. Let’s take an example, zxe66exz is a palindrome. Reversing this string reverts to the real string zxe66exz. The string 23lmxs32 is not a palindrome. Conversely, the new string will be 23sxml32, and it is dissimilar from the real string.

This article describes the different approaches we can use in Python to verify whether a string is a palindrome or not.

Use List Slice Method:

A palindromic string contains a value that is read back and forth in an identical way. For instance, the string “rotor” remains unchanged even if the order of the characters in it is reversed. This sort of string is called a palindrome. We search for a palindromic string by inverting the real string and then matching every character of the real string with every character of the inverted string. This is done using the list slices. The succeeding code shows how to utilize the list slice technique to see if a defined string is a palindrome.

We must install the Spyder program version 5 to use the Python codes. By selecting “new file” from the menu bar, we may establish a new coding project. “” is the title of a new file:

Let’s put the code to the test. To run this code, go to the menu bar of Spyder5 and tap the “Run” feature:

Here, we used the list slice process to check if the string “civic” is a palindrome. First, we used [:: 1] as the list of the index to determine the reciprocal of the real string. Then, we matched every index of the string using the “==” equation operator. When the real string and the inverted string are compared, it prints the palindrome.

Use checkPalindrome() Method:

In this case, we utilize the checkPalindrome() function. This user-defined method takes a value and returns one if the reciprocal is identical to the element itself (the value of the argument). Hence, we accepted the number given by the user as a parameter to this method. Therefore, we initialize the return value with the variable “chk”. When we compare the values ​​of the variable “chk”, if it holds 1, it shows that the given value is a palindromic number. Else, the entered value is not palindromic:

In this example, the user enters the number “0983527” to check if it is palindrome or not. We reverse the numbers, and after this, we compare the reversed numbers to the original numbers. They both are not the same. So it prints “983527 is not a palindrome number” as an output:

Using the reversed() Method:

The reversed() function in Python is used to determine whether or not a provided string or value is a palindrome. The reversed() method takes a series of characters and returns the inverse iterator of that series. So a string contains a series of different characters, we utilize the reversed() method instead of the list catalog [::-1] to inverse the series of values in the string. Then, we relate mutually the real and inverted strings element by element to decide if it is a palindrome or not:

The preceding program shows how to use the reversed() method to check if a sequence is a palindrome:

We used the reversed() method in the above program to see if the string “deified” is a palindrome. First, we utilize the “.join (reversed () function” to analyze the inverse form of the real string. Then we used the “==” equation operator to associate both the real string and the inverse string, element by element. When both the real string and the reverse string match, print a palindrome.


A palindrome is a collection of words, numbers, or other series of characters that are read back and forth in a similar way. It makes no difference to read the string from the first letter or in the opposite direction. In this article, we discussed the three methods that are used to check if the defined string or number is a palindrome or not. These techniques include the use of the checkPalindrome() method, the use of the reversed() method, and the use of the list slice method. The algorithm is used to determine whether or not a string or integer is a palindrome. First, read the given string or number. Then, store that number or string in a transient variable. Now, we invert the defined string or number. After this, we match that transient variable with the reversed string or number. When the reversed number or string is identical to the defined number or string, the program prints “palindrome” as an output. Otherwise, prints “not a palindrome”. We hope you found this article helpful. Check out other Linux Hint articles for more tips and information.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.052088975906372