Program to check whether a String is a Palindrome or not in Python

What are Palindromes?

A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. They can be both numeric and characters. For instance,

  • Numeric palindromes are the number that, when reversed, have the same chronological order of their digits. For instance,

         12021 is a string that when reversed reads the same i.e 12021. Therefore it’s a palindrome.       

such palindromes are numeric palindromes.

Checking for Palindrome

Program to check for palindromes in python

A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. They can be both numeric and characters. For instance,

  • Numeric palindromes are the number that, when reversed, have the same chronological order of their digits. one such example is the date/time palindrome.

         02/02/2020 is a date type palindrome which when reversed gives 02/02/2020 while in DD/MM/YYYY format.

         such palindromes are numeric palindromes.

  • Non-Numeric or character string palindromes are the words or sentences that, when reversed, has the same chronological order of characters and words. one such example is anna.

          When we reverse the letters of the word anna, we get the same word. therefore it’s a palindrome.

There are two approaches to check whether a string is a palindrome or not, the approaches are as follows,

  1. Using Brute force
  2. Using string slicing
Checking for palindrome

Method 1: Using Brute force

Python Code
string = input().lower()
size = len(string)
if size%2==0:
  strn=''
  for i in range(int(size/2)):
    strn+=string[i]
  if strn+strn[::-1]==string:
    print("It's a palindrome")
  else:
    print("It's not a palindrome")
else:
  strn=''
  for i in range(int(size/2)):
    strn+=string[i]
  if strn+string[i+1]+strn[::-1]==string:
    print("It's a palindrome")
  else:
    print("It's not a palindrome")
Output
anna
It's a palindrome

Explanation

The task is to check whether the string input is a palindrome or not. In the above code we traverse through the string input and reverse the elements of the string. If the reversed string is the same as the original one. It’s a palindrome. 

The algorithm for the above code is as follow:

  1. Take string input using input() function.
  2. check if the size of the string is even or odd.
  3. If even, iterate through the string using a for loop until Length/2.
  4. Append the values in a string variable and concatenate the other half after reversing the characters. If it’s the same as the original, it’s a palindrome.
  5. if the size of the string is odd. iterate through the loop until Length/2. append all the values in a string variable.
  6. concatenate the substring, middle element and the reverse of the substring. If it matches the original string. It’s a palindrome.
  7. Print whether or not the input string is a palindrome.

The output is a string saying it’s a palindrome if True or It’s not a palindrome otherwise.

Method 2: Using String Slicing

Python Code
string = input().lower()
if string == string[::-1]:
  print("It's a palindrome")
else:
  print("It's not a palindrome")
Output
anna
It's a palindrome

Explanation

The task is to check whether the string input is a palindrome or not. In the above code we traverse through the string input and reverse the elements of the string. If the reversed string is the same as the original one. It’s a palindrome. 

The algorithm for the above code is as follow:

  1. Take string input using the input() function.
  2. Reverse the string using string slicing string[::-1] and check if it matches the original string.
  3. If it matches, print it’s a palindrome. print it’s not a palindrome otherwise.

The output is a string saying it’s a palindrome if True or It’s not a palindrome otherwise.

For more solutions, click the button.