Python Program to Check a Given String is Palindrome

Write a Python Program to Check whether a Given String is Palindrome or Not with a practical example. A string could be a Palindrome string in Python if it remained the same after reversing it.

Python Program to Check a Given String is Palindrome or not using slice

This program allows the user to enter a string. Next, we used the If statement to check whether the given string is equal to the reverse of that or not. If it is True, Palindrome string in Python; otherwise, not.

st[:: – 1] returns the string in reverse order. Please refer String article to understand everything about it in Python.

st = input("Please enter your own text : ")

if(st == st[:: - 1]):
   print("This is a Palindrome String")
else:
   print("This is Not")
Python Program to Check a Given String is Palindrome or Not 1

Python Program to Check if a Given String is Palindrome using for loop

In this Python program, we used For Loop to iterate every character in a String. Inside the For Loop, we are assigning each character to str1 (before). Next, we used the If statement checks the palindrome string.

string = input("Please enter your own Text : ")
str1 = ""

for i in string:
    str1 = i + str1  
print("Reverse Order :  ", str1)

if(string == str1):
   print("This is a Palindrome String")
else:
   print("This is Not")
Please enter your own Text : aabbcc
Reverse Order :   ccbbaa
This is Not
>>> 
Please enter your own Text : aabbaa
Reverse Order :   aabbaa
This is a Palindrome String
>>> 

It is a more traditional or old approach to finding whether the given string is a palindrome or not using for loop.

string = input("Please enter your own : ")
flag = 0

length = len(string)
for i in range(length):
    if(string[i] != string[length - i - 1]):
        flag = 1
        break

if(flag == 0):
   print("This is True")
else:
   print("This is Not")
Please enter your own : aabbcbbaa
This is True
>>> 
Please enter your own : tutorialgateway
This is Not

Python Program to Check if String is Palindrome using while loop

def ispalindrome(s):
    i = 0
    while i < len(s)/2:
        if s[i] != s[-i -1]:
            return False
        i += 1
    return True

st = input("Enter Text = ")

if ispalindrome(st):
    print("Palindrome String.")
else:
    print("Not.")
Enter Text = dad
Palindrome String.

Enter Text = hello
Not.

Python Program to Check Palindrome String using functions

In this program, the for loop iterates the string from first to last. The if str[i] != str[len(str)-i-1] checks whether the first character equals the last character. If true, it checks the second character with the last but one, and so on.

def is_palindrome(str):
    for i in range(len(str)):
        if str[i] != str[len(str)-i-1]:
            return False
    return True

str = input("Enter Text = ")
if is_palindrome(str):
    print("palindrome")
else:
    print("Not")

Python Program to Check String is Palindrome or not using Recursion

In this palindrome program, we are using the len function to find the string length. Next, we used recursive Functions to call the function recursively.

def ispalindrome(str1):
    if(len(str1) == 0):
        return str1
    else:
        return ispalindrome(str1[1 : ]) + str1[0]
    
s = input("Please enter your own : ")
str1 = ispalindrome(s)
print("String in reverse Order :  ", str1)

if(s == str1):
   print("This is a Palindrome String")
else:
   print("This is Not")
Please enter your own : wow
This is a Palindrome String
>>> 
Please enter your own : python
This is Not

You can also write the above program

def ispalindrome(str):
    if len(str) <= 1:
        return True
    if str[0] != str[-1]:
        return False
    return ispalindrome(str[1:-1])


s = input("Please enter your own : ")

if (ispalindrome(s)):
    print("It is a Palindrome String")
else:
    print("This is Not")
Please enter your own : mom
It is a Palindrome String

Python Palindrome String Program using functools reduce

It imports the reduce function from the functools library and uses the lambda, reversed function.

from functools import reduce

def ispalindrome(s):
    return reduce(lambda x, y: x + y, reversed(s)) == s

st = input("Enter Text = ")

if ispalindrome(st):
    print("Palindrome String.")
else:
    print("Not.")
Enter Text = mom
Palindrome String.

Using collections deque

from collections import deque

def ispalindrome(s):
    dq = deque(s)

    while len(dq) > 1:
        if dq.popleft() != dq.pop():
            return False
    return True


st = input("Enter Text = ")

if ispalindrome(st):
    print("Palindrome String.")
else:
    print("Not.")
Enter Text = radar
Palindrome String.

Enter Text = car
Not.