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 – 

Checking for Palindrome

Write a program to Check whether a String is Palindrome or not in python

Methods discussed

  1. Method 1: Using String Slicing
  2. Method 2: Matching ith character from front-back
  3. Method 3: Matching ith character from front-back (Updated)
  4. Method 4: Inbuilt function
  5. Method 5: Building using arithmetic
  6. Method 6: Building using flag
  7. Method 7: Negative Traversal in loop
Program to Check given String is Palindrome or Not in Python

Method 1

This method uses negative slicing to generate reverse of string

Run
# Write a program to check whether a string is palindrome or not in python
input_string = 'civic'
rev = input_string[::-1]

if input_string == rev:
    print(rev + " is Palindrome")
else:
    print(rev + " is not Palindrome")

Output

civic is Palindrome

Method 2 (Matching ith character from front-back)

For string check, if each i-th character from the front is the same as i-th character from back or not.

If yes then its a palindrome

Python Code
Run
def isPalindrome(input_str):

    # check if input_str[i] is same as input_str[len(str) - i - 1]
    # for each char in string
    for i in range(0, len(input_str)):

        # Essentially, what is being checked isif i-th character is
        # equal to i-th character from the end or not
        if input_str[i] != input_str[len(input_str) - i - 1]:
            return False

    return True

input_str = "civic"

print("Palindrome") if isPalindrome(input_str) else print("Not Palindrome")

Output

civic is Palindrome

Method 3 (Matching ith character from front-back, updated)

It’s very similar to the previous method, with one difference that –

Rather than checking ith character from the front is the same as the ith character from the back for the whole string.

We just check for the first half of the string, since, if the string is Palindrome. The first half and second half when read backwards will anyways be the same.

Python Code
Run
def isPalindrome(input_str):
    # Run loop from 0 to len/2
    mid = int(len(input_str) / 2)
    for i in range(0, mid):

        # Essentially, what is being checked isif i-th character is
        # equal to i-th character from the end or not
        if input_str[i] != input_str[len(input_str) - i - 1]:
            return False

    return True


input_str = "civic"

print("Palindrome") if isPalindrome(input_str) else print("Not Palindrome")

Output

civic is Palindrome

Method 4 (inbuilt function)

We will use inbuilt reversed function to do it in this method

Run
# main function
string = "civic"

reverse = ''.join(reversed(string))

print(string + " is: ", end="")
print("Palindrome") if string == reverse else print("Not Palindrome")

Output

civic is: Palindrome

Method 5 (Building reverse using arithmetic)

We build the reverse using arithmetic here

Run
input = "civic"
# this will automatically generate reverse
rev = ""
for ch in input:
    rev = ch + rev

print(input + " is : ", end="")
print("Palindrome") if input == rev else print("Not Palindrome")

print("string: " + input)
print("rev: " + rev)

Output

civic is : Palindrome
string: civic
rev: civic

Method 6 (Building reverse using flag)

We build the reverse using flag here

Run
input = "civic"

j = -1
flag = 0
for char in input:
    # char starts from index 0
    # string[j] forces to read from end
    # bcz negative index are read from end
    if char != input[j]:
        flag = 1
        break
    j = j - 1

print(input + " is : ", end="")
print("Not Palindrome") if flag else print("Palindrome")

Output

civic is : Palindrome

Method 7 (Negative traversal in Loop)

We build the reverse using Negative traversal in Loop

Output

civic is : Palindrome
Run
input = "civic"
n = len(input)
revlist = []

for i in range(n - 1, -1, -1):
    revlist.append(input[i])

rev = "".join(revlist)

print(input + " is: ", end="")
if input == rev:
    print("Palindrome")
else:
    print("Not Palindrome")
  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python

One comment on “Program to check whether a String is a Palindrome or not in Python”


  • nidhisingh151199

    s=input()
    s1=””
    for i in s:
    s1=i+s1
    print(s1)
    if(s==s1):
    print(“Palindrome”)
    else:
    print(“Not Palindrome”)