Python Palindrome Detection

Python Palindrome Detection

Python Palindrome Detection

 Hello everyone, in this post I will explain a few different methods you can use to check if a number or a word is a palindrome using Python. Happy reading.

Method 1 Using While

In this method, we use While to rewrite the word or number in reverse and then make a comparison.

  
def WhilePalindrome(value):
    newValue = "" # We created a variable to hold the new word
    counter = len(value) -1 # We created a counter to control the while
    while counter >= 0: # We started the while loop to read in reverse
        newValue += value[counter] # We added our letter to the new value
        counter -= 1 # We decreased our counter
    if newValue == value: # If the new value is the same as the old value
        return True # We returned True
    else: # If not the same
        return False # We returned False

  

Method 2 Using For

In this method, we print the word in reverse with For and compare it just like we do with while.

  
def ForPalindrome(value):
    newValue = "" # We created a variable to hold the new word
    for i in range(len(value) -1, -1, -1): # We created a reversing for loop to write the entered value backwards
        newValue += value[i] # We added the last letter to the new value
    if newValue == value: # If the new value is the same as the old value
        return True # We returned True
    else: # If not the same
        return False # We returned False
  

Method 3 Using [::-1]

This method is one of the blessings of Python language, we use the "[::-1]" method which we use to reverse a list, and very easily check if something is a palindrome.

def PalindromeCheck(value):
    return value == value[::-1] # We check if our value is the same as its reversed version, in Python this will automatically return True if the same, otherwise False.

Let's Look at All Our Code

    
def ForPalindrome(value):
    newValue = "" # We created a variable to hold the new word
    for i in range(len(value) -1, -1, -1): # We created a reversing for loop to write the entered value backwards
        newValue += value[i] # We added the last letter to the new value
    if newValue == value: # If the new value is the same as the old value
        return True # We returned True
    else: # If not the same
        return False # We returned False

def WhilePalindrome(value):
    newValue = "" # We created a variable to hold the new word
    counter = len(value) -1 # We created a counter to control the while
    while counter >= 0: # We started the while loop to read in reverse
        newValue += value[counter] # We added our letter to the new value
        counter -= 1 # We decreased our counter
    if newValue == value: # If the new value is the same as the old value
        return True # We returned True
    else: # If not the same
        return False # We returned False

def PalindromeCheck(value):
    return value == value[::-1] # We check if our value is the same as its reversed version, in Python this will automatically return True if the same, otherwise False.

# Usage

forCheck = ForPalindrome("ata")
whileCheck = WhilePalindrome("ata")
palindromeCheck = PalindromeCheck("ata")

print(forCheck, whileCheck, palindromeCheck)
# Output "True True True"
    

If you have any questions or if there is anything you are stuck on about the post, you can use the comments section below to ask your question.