DEV Community

Vaarun Sinha
Vaarun Sinha

Posted on • Edited on

Python Program To Find If A word/number is a palindrome or not.

What Is A Palindrome?

Like every programmer does, let's google it!
πŸ˜‰
googles
A word, phrase, or sequence that reads the same backwards as forwards, e.g madam or nurses run.

Problem

To find if a word/number is a palindrome or not.

Sub Problems

  • Get user input.
  • Check if the input is an integer or string.
  • Reverse the input.
  • Check if the initial input is same as the reversed one.

Pseudo Code

Input = GetUserInput()
reversed_input = reverse(input)

if Input is equal to reversed_input:
print("It Is A Palindrome Number")
else:
print("it is not a palindrome number")
Enter fullscreen mode Exit fullscreen mode

Let's Finally Code It!

  • Get User Input
userInputw = input("Enter The Value:\n") #w for with whitespace
userInput = userInputw.replace(" ", "") #removing whitespace so we can test for phrases like nurses run.
Enter fullscreen mode Exit fullscreen mode
  • Reverse The Input
reversedInput = userInput[::-1]
Enter fullscreen mode Exit fullscreen mode
  • Check If The Initial Input is same as the reversed one.

if reversedInput == userInput:
    print(f"{userInput} is equal to it's reverse {reversedInput} therefore it is a palindrome number.")
else:
     print(f"{userInput} is not equal to it's reverse {reversedInput} therefore it is not a palindrome number.")
Enter fullscreen mode Exit fullscreen mode

Making the Code Reusable

  • To make the code reusable let's put the logic in a function.

def reverse_value(value):
 reversedValue = value[::-1]
 return reversedValue

def is_palindrome(value):
   reversedValue = reverse_value(value)
    if reversedValue == value:
        return True
    else:
         return False

Enter fullscreen mode Exit fullscreen mode

Full Code

def reverse_value(value):
 reversedValue = value[::-1]
 return reversedValue

def is_palindrome(value):
    reversedValue = reverse_value(value)
    if reversedValue == value:
        return True
    else:
         return False

userInputw = input("Enter The Value:\n")

userInput = userInputw.replace(" ", "")

isPalindrome = is_palindrome(userInput)

if isPalindrome:
    print(f"{userInputw} is equal to it's reverse {userInputw} therefore it is a palindrome.")
else:
     print(f"{userInput} is not equal to it's reverse {reverse_value(userInput)} therefore it is not a palindrome.")

Enter fullscreen mode Exit fullscreen mode

Key Takeaways:

  • [::-1] Reverses a string.
  • string.replace(" ", "") replaces all whitespaces with no whitespaces.
  • Before jumping right into code, break the problem Into sub problems then do some pseudo code, then write the code. After That You can optimise it.
  • Implement this if the problem is hard or you cannot exactly think what to code. or else it is a time waster for easy problems.
  • Make The Code Reusable.

Run it on repl

Github: https://github.com/Dev2212/Palindrome-Finder-In-Python

HAPPY CODING!

Top comments (1)

Collapse
 
vaarun_sinha profile image
Vaarun Sinha • Edited

Feedback is highly appreciated! This is my first post on dev.to hope you enjoy it and I hope you could learn something new from it.