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 python 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 string or not. If it is True, Palindrome string; otherwise, not a palindrome string in Python.

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

# Python Program to Check a Given String is Palindrome or Not

string = input("Please enter your own String : ")

if(string == string[:: - 1]):
   print("This is a Palindrome String")
else:
   print("This is Not a Palindrome String")
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.

# Python Program to Check a Given String is Palindrome or Not

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

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

if(string == str1):
   print("This is a Palindrome String")
else:
   print("This is Not a Palindrome String")

Python palindrome string output

Please enter your own String : aabbcc
String in reverse Order :   ccbbaa
This is Not a Palindrome String
>>> 
Please enter your own String : aabbaa
String in 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.

# Python Program to Check a Given String is Palindrome or Not

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

if(string == str1):
   print("This is a Palindrome String")
else:
   print("This is Not a Palindrome String")

Python palindrome string output

Please enter your own String : wow
This is a Palindrome String
>>> 
Please enter your own String : python
This is Not a Palindrome String
>>> 

Python Program for Palindrome String Example 4

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

# Python Program to Check a Given String is Palindrome or Not

string = input("Please enter your own String : ")
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 a Palindrome String")
else:
   print("This is Not a Palindrome String")

Python palindrome string output

Please enter your own String : aabbcbbaa
This is a Palindrome String
>>> 
Please enter your own String : tutorialgateway
This is Not a Palindrome String
>>>