Validate Palindrome Words using Python

Palindrome words are words that are read the same backward as forward. For example, mom, dad, and madam are palindrome words. Validating palindrome words is one of the popular coding interview questions. So, if you want to learn how to validate palindrome words, this article is for you. In this article, I will take you through how to validate palindrome words using Python.

Validate Palindrome Words using Python

A phrase, word, or number is a palindrome if it reads the same backward as forward. For validating palindrome words, you will be given words or phrases as input, and you have to check if it is palindrome or not. If it’s in a palindrome sequence, you need to return True, else False.

For example, have a look at the input and output of this problem below:

Input: “A man, a plan, a canal: Panama”
Output: True

I hope you now have understood what are palindrome words. Now, below is how you can validate palindrome words using the Python programming language:

def ispalindrome(x):
    x = x.lower()
    text = ""
    for i in range(len(x)):
        if x[i].isalnum():
            text = text + x[i]
    return text == text[::-1]

print(ispalindrome("A man, a plan, a canal: Panama"))
Output: True

I hope you now have understood how to validate palindromes using Python. You can find many more practice questions to improve your problem-solving skills using Python here.

Summary

A phrase, word, or number is a palindrome if it reads the same backward as forward. For validating palindrome words, you will be given words or phrases as input, and you have to check if it is palindrome or not. I hope you liked this article on validating palindrome words using Python. Feel free to ask valuable questions in the comments section below.

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1297

Leave a Reply