Python Program to Check a Given String is Palindrome

Write a Python Program to Check 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 Example 1

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 find a Given String is Palindrome Example 2

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 If statement checks the palindrome string in python.

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
>>> 

Python Program to Check String is Palindrome or not Example 3

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

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

if(string == 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

Example 4

It is a more traditional or old approach to find the given string is a palindrome or not.

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