A palindrome is a word that is the same whether you read it forwards or backward.

Checking whether an input string is a palindrome can be a common interview question that you may encounter at least in one of the technical rounds of your interviews.

Fortunately, we can do it very quickly in Python.

Logically, we only need to check whether the value of a string is equal to its reversed version, i.e.:

string == reversed_string

We can reverse a string using the [::-1] index in front of it:

def is_palindrome(input_string):
   return input_string == input_string[::-1]


ab_string = is_palindrome("AB")
print(ab_string)  # False

Now, let us call it using AB as an input string:

ab_string = is_palindrome("AB")
print(ab_string)  # False

Since it is not equal when read forward and backward, then it is not a palindrome.

Here is another example:

aba_string = is_palindrome("aba")
print(aba_string)  # True

This returns True, since it is indeed a palindrome.

That’s basically it.

I hope you find it helpful.