Python program to check if a string is palindrome or not

1 min read

Problem Definition

Make a Python program to check if the given string is palindrome or not. A string is a palindrome if it reads same from forward as well as backward for example madam.

Solution

  1. Take the input string from the user and store it in a variable
  2. Reverse the string and compare it to the base string
  3. Print the result
  4. End

Program

word = input("Enter a number : ")
word = word.casefold()

if(word == word[: : -1]):
    print("Palindrome")
else:
    print("Not Palindrome")

Runtime Test Cases

Enter a number : madam
Palindrome

Additional Explanation

The casefold() method is used to implement caseless string matching. It is similar to lower() string method but case removes all the case distinctions present in a string. i.e ignore cases when comparing hence this program will work for both the following strings Madam and madam.


PROGRAMS
author's image
Abhijeet Pal Author and Editor in Chief @djangocentral

Abhijeet is a full-stack software developer from India with a strong focus on backend and system design. He is driven by the need to create impactful solutions that add value to the internet in any way possible.

LinkedIn Twitter Github

Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read