Python
Python

 

Introduction

A palindrome is a word, phrase, or sequence of characters that reads the same forwards and backwards,
ignoring spaces, punctuation, and capitalization. For example, “madam” and “racecar” are palindromes,
while “hello” and “world” are not.

In this program, we will write a Python function that checks whether a given string is a palindrome.
We will compare the original string with its reversed version to determine if they are identical.

Objective

The objective of this program is to demonstrate how to check if a given string is a palindrome using Python.
We will focus on:

  • Reading an input string from the user.
  • Normalizing the string to ignore spaces, punctuation, and case differences.
  • Comparing the string with its reversed version to determine if it is a palindrome.

Python Code

def is_palindrome(s):
    # Normalize the string: remove non-alphanumeric characters and convert to lowercase
    s = ''.join(c.lower() for c in s if c.isalnum())
    
    # Check if the string is equal to its reverse
    return s == s[::-1]

# Get user input
input_string = input("Enter a string to check if it's a palindrome: ")

# Check and display result
if is_palindrome(input_string):
    print(f"'{input_string}' is a palindrome.")
else:
    print(f"'{input_string}' is not a palindrome.")

Program Explanation

Let’s break down the program step by step:

  1. is_palindrome function:
    – This function accepts a string s as its input.
    – It uses a list comprehension to filter out non-alphanumeric characters (e.g., spaces, punctuation) and converts all characters to lowercase for uniformity. This ensures that the check is case-insensitive and ignores irrelevant characters.
    – Then, the function compares the string s with its reverse s[::-1]. The slicing technique s[::-1] creates a reversed version of the string. If the original string matches the reversed string, it is a palindrome.
  2. User Input:
    – The program prompts the user to enter a string. The input() function captures the user’s input.
  3. Palindrome Check:
    – The string entered by the user is passed to the is_palindrome() function. The function returns True if the string is a palindrome and False otherwise.
  4. Output:
    – Based on the result from is_palindrome(), the program prints whether the string is a palindrome or not.

How to Run the Program

To run the program, follow these steps:

  1. Ensure you have Python installed on your computer. You can download it from python.org.
  2. Save the Python code in a file with a .py extension (e.g., palindrome_checker.py).
  3. Open a terminal or command prompt and navigate to the directory where your Python file is located.
  4. Run the program by typing python palindrome_checker.py in the terminal or command prompt.
  5. The program will prompt you to enter a string. After entering the string, it will display whether it is a palindrome or not.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)