Search

## Python Program: Find Palindrome Number in Python

In this article learn how to identify palindrome numbers using Python, with step-by-step explanations and optimization techniques. Discover the mathematical and programming concepts behind palindrome numbers.

A palindrome number is a number that remains the same when its digits are reversed. In other words, if you read the number from left to right or right to left, it will still be the same number.

For example, 11, 121, 12321, and 3443 are palindrome num.

```				```
word = input("Enter a word: ")

# Remove whitespace and convert to lowercase
word = word.replace(" ", "").lower()

# Reverse the word
reversed_word = word[::-1]

# Check if the original and reversed words are equal
if word == reversed_word:
print("It is a palindrome.")
else:
print("It is not a palindrome.")

```
```
In this example, the program prompts the user to enter a word. It removes any whitespace and converts the word to lowercase using the replace() and lower() methods. It then uses string slicing ([::-1]) to reverse the word and stores it in the reversed_word variable. Finally, it compares the original word and the reversed word to determine if they are equal and prints the result accordingly.
```				```
num = 121
palindrome_number = num
reverse_num = 0
while num > 0:   #121, 12, 1
digit = num % 10   #1, #2, #1
reverse_num = (reverse_num*10)+digit   #(0*10)=0+1=1, #(1*10)=10+2=12, #(12*10)=120+1=121
num = num//10   #121//10 = 12, #12//10 = 1, #1//10 = 0

if palindrome_number == reverse_num:
print(True)
else:
print(False)
```
```
The program starts by storing the number in the variable “num”. The program then stores the original number for comparison. It uses a loop to reverse the number by extracting the last digit using the modulus operator (`%`) and adding it to the `reverse_num` variable. The loop continues until the original number becomes zero. Finally, it compares the original number and the reversed number to determine if they are equal and prints the result accordingly.
```				```
def is_palindrome(string):
# Remove whitespace and convert to lowercase
string = string.replace(" ", "").lower()

# Reverse the string
reversed_string = string[::-1]

# Check if the original and reversed strings are equal
if string == reversed_string:
return True
else:
return False

# Example usage
word = input("Enter a word: ")
if is_palindrome(word):
print(word,"-is a palindrome.")
else:
print(word,"-is not a palindrome.")
```
```
In this program, the is_palindrome function takes a string as input. It removes any whitespace and converts the string to lowercase to make the palindrome check case-insensitive. It then uses string slicing ([::-1]) to reverse the string and stores it in the reversed_string variable. Finally, it compares the original string and the reversed string to determine if they are equal.
```				```
def is_palindrome(number):
# Convert the number to a string
number_str = str(number)

# Reverse the string
reversed_str = number_str[::-1]

# Check if the original and reversed strings are equal
if number_str == reversed_str:
return True
else:
return False

# Example usage
num = int(input("Enter a number: "))
if is_palindrome(num):
print(num,"is a palindrome.")
else:
print(num,"is not a palindrome.")
```
```
In this program, the is_palindrome function takes a number as input. It converts the number to a string using the str() function. Then, it uses string slicing ([::-1]) to reverse the string and stores it in the reversed_str variable. Finally, it compares the original string and the reversed string to determine if they are equal.

### Conclusion

In this article, we explored how to identify palindrome numbers using Python. Which is the concept of reading the same number from backward as forward.

We learned that palindrome numbers can be determined by comparing the original number with its reverse. By utilizing string manipulation techniques and mathematical operations, we can easily check whether a given number is a palindrome or not.

We discussed various approaches to solve this problem, including converting the number to a string and comparing it with its reversed version, or using mathematical operations to extract digits and construct a reverse number for comparison.

Overall, by mastering the techniques discussed, you’ll be well-equipped to tackle palindrome number problems and further develop your programming skills.

Recent Articles