## DEV Community

Vaarun Sinha

Posted on • Updated 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!
๐
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")
``````

## 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.
``````
• Reverse The Input
``````reversedInput = userInput[::-1]
``````
• 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.")
``````

## 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

``````

## 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.")

``````

## 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.

HAPPY CODING!