Palindrome Program in Python: Check if a Number or String is Palindrome
Palindrome Program in Python: Check if a Number or String is a Palindrome
A palindrome is a word, phrase, number, or sequence that reads the same backward as forward. Whether it’s a number like 121 or a word like madam, a palindrome exhibits symmetry. In this guide, we’ll explore how to check if a given number or string is a palindrome using Python. This fundamental exercise in programming helps you practice key skills like loops, string manipulation, and functions.In this article, you will learn how to:
Check if a number is a palindrome.
Check if a string is a palindrome.
Use Python loops, slicing, and functions to perform these checks.
What is a Palindrome?
A palindrome is a sequence that remains the same when reversed. Common examples of palindromes include:
Words: “madam”, “racecar”
Numbers: 121, 434
Phrases: “step on no pets” (ignoring spaces and punctuation)
Now, let’s dive into checking whether a number or string is a palindrome in Python.
How to Check if a Number is a Palindrome in Python
Sample Input:
mathematica
Enterthenumber:454
Sample Output:
Palindrome
Algorithm for Palindrome Check on Numbers
Input the number from the user.
Reverse the number and compare it to the original number.
If both are the same, print “Palindrome”.
If not, print “Not a Palindrome”.
Method 1: Using a While Loop
In this method, we use a while loop to reverse the number and check if it matches the original.
python
# Python program to check if the number is a palindrome using a while loopn = int(input(“Enter the number: “))
reverse = 0
number = n# Reversing the given numberwhile n != 0:
remainder = n % 10
reverse = reverse * 10 + remainder
n = n // 10# Check if the original number equals the reversed numberif number == reverse:
print(“Palindrome”)
else:
print(“Not a Palindrome”)
Input:
mathematica
Enterthenumber:454
Output:
Palindrome
Method 2: Using a Function
Alternatively, we can use a function to reverse the number and check if it’s a palindrome.
python
# Python program to check if the number is a palindrome using a functiondefrev(temp):
reverse = 0while temp != 0:
remainder = temp % 10
reverse = reverse * 10 + remainder
temp = temp // 10return reverse# Main function
n = int(input(“Enter the number: “))
temp = n
res = rev(temp)if res == n:
print(“Palindrome”)
else:
print(“Not a Palindrome”)
Input:
mathematica
Enterthenumber:759
Output:
css
Not a Palindrome
How to Check if a String is a Palindrome in Python
Now let’s explore how to check if a string is a palindrome. We’ll demonstrate two methods: one using slicing and another using a while loop.
Sample Input:
c
Enter the string: MADAM
Sample Output:
Palindrome
Algorithm for Palindrome Check on Strings
Get the string from the user.
Reverse the string and compare it to the original.
If both are the same, print “Palindrome”.
If not, print “Not a Palindrome”.
Method 1: Using Slicing
Slicing is a simple way to reverse a string and check for a palindrome in Python. Here’s how it’s done:
python
# Python program to check if the string is a palindrome using slicingmyStr = input(“Enter the string: “)
if myStr == myStr[::-1]: # Checking if the string is equal to its reverse using slicingprint(“Palindrome”)
else:
print(“Not a Palindrome”)
Input:
c
Enter the string: Welcome
Output:
css
Not a Palindrome
Method 2: Using a While Loop
In this method, we use a while loop to reverse the string manually and check if it’s a palindrome.
python
# Python program to check if the string is a palindrome using a while loopmyStr = input(“Enter the string: “)
rev = “”
j = myStr# Reverse the string using a while loopwhilelen(j) > 0:
rev = rev + j[-1]
j = j[:-1]if rev == myStr:
print(“Palindrome”)
else:
print(“Not a Palindrome”)
Input:
vbnet
Enter the string: stepon no pets
Output:
Palindrome
Key Takeaways
Palindromes are sequences that read the same forwards and backwards.
Python provides several methods to check for palindromes, including while loops and slicing.
Understanding how to reverse numbers and strings is a fundamental skill that helps in problem-solving and coding interviews.
Conclusion
Palindrome checking is a great way to sharpen your skills in Python, especially with loops, string manipulation, and functions. Whether you’re checking numbers or strings, the techniques provided in this article will help you understand the core principles of programming while preparing you for coding challenges and interviews.
Enhance Your Programming Skills with FACE Prep’s CRT Program
If you’re preparing for campus recruitment and want to strengthen your coding skills, FACE Prep’s Campus Recruitment Training (CRT) Program is the perfect way to prepare. With expert-led courses in coding, reasoning, aptitude, and interview preparation, FACE Prep offers a comprehensive curriculum designed to help you ace your IT placement interviews.