Palindrome Program in Python: A Complete Guide

In this Post Palindrome Program in Python, we'll explore how to write Python Programs to check palindrome , using multiple approaches.

Palindrome Program in Python: A Complete Guide

Getting Started

Palindromes are often used as exercises in programming to practice string manipulation, loops, and conditionals. A palindrome is a word, phrase, number, or sequence that reads the same backward as forward, ignoring spaces, punctuation, and capitalization. Some common examples include:

  • Words: noton, rabar, civic etc.
  • Numbers:191, 181, 171 etc.
  • Phrases: No lemon, no melon (ignoring spaces and punctuation)

What Is a Palindrome?

Palindrome Meaning:- A palindrome is a word, number, phrase, or sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.

Palindrome Code in Python

Here we are going to share following palindorme exampls for :
  1. Workds or String: To check if the characters are the same when reversed.
  2. Numbers: To check if the digits are the same when reversed.
  3. Phrases:

Check Palindrome string in Python
Here's a simple python palindrome program that checks if a string is a palindrome:
 def is_palindrome(n):  
   original = n  
   reversed_num = 0  
   while n > 0:  
     digit = n % 10  
     reversed_num = reversed_num * 10 + digit  
     n = n // 10  
   return original == reversed_num  
 # Example usage  
 print(is_palindrome(1331)) # True  
 print(is_palindrome(1234)) # False  

Check Palindrome Word With Python Method
 def is_palindrome(s):  
   s = s.lower()  
   return s == s[::-1] 
   
   # Example usage  
 word = input("Enter a word: ")  
 if is_palindrome(word):  
   print(f"'{word}' is a palindrome.")  
 else:  
   print(f"'{word}' is not a palindrome.")  

Explanation:
  • s[::-1] is Python's slicing technique to reverse a string.
  • We compare the original string to its reversed version.

Check Palindrome number in Python
 def is_palindrome_number(num):  
   # Convert number to string  
   num_str = str(num)  
   # Compare the string with its reverse  
   return num_str == num_str[::-1]  
 # Example usage:  
 number = int(input("Enter a number: "))  
 if is_palindrome_number(number):  
   print(f"{number} is a palindrome.")  
 else:  
   print(f"{number} is not a palindrome.")  

Palindrome Check Using Arithmetic
 def is_palindrome_number(num):  
   original = num  
   reversed_num = 0  
   while num > 0:  
     digit = num % 10  
     reversed_num = reversed_num * 10 + digit  
     num = num // 10  
   return original == reversed_num  
 # Example usage  
 number = int(input("Enter a number: "))  
 if is_palindrome_number(number):  
   print(f"{number} is a palindrome.")  
 else:  
   print(f"{number} is not a palindrome.")  

How it works:
  • Extracts the last digit using % 10.
  • Builds the reversed number step by step.
  • Compares the reversed number to the original.

Palindrome Phrase Checker

To check whether a phrase is a palindrome in Python (ignoring spaces, punctuation, and case), you can use the following function:

 import string  
 def is_palindrome_phrase(phrase):  
   # Remove punctuation and spaces, and convert to lowercase  
   cleaned = ''.join(char.lower() for char in phrase if char.isalnum())  
   # Compare cleaned string with its reverse  
   return cleaned == cleaned[::-1]  
 # Example usage  
 phrases = [  
   "A man, a plan, a canal: Panama",  
   "Was it a car or a cat I saw?",  
   "No lemon, no melon",  
   "This is not a palindrome"  
 ]  
 for p in phrases:  
   result = is_palindrome_phrase(p)  
   print(f"'{p}' → Palindrome: {result}")  

Summary

In this post, we learned how to write a Palindrome Program in Python, a common task in programming interviews, coding practice, and text processing.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال