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

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read