String Palindrome program in python

In this tutorial, you will learn the writing Python program to check if a string is a palindrome or not.

 

Read This: What is String Palindrome? Write string palindrome program in C.

To check String is Palindrome or not?  This Program will take a String as an input. And after applying some logic it will return output as String is Palindrome or not.

For Example: 

Suppose if we give input a string “madam”. This is palindrome String then our program will print “Given string is palindrome”.

And if we give “abcd” then our program will give “Given String is not palindrome”.

Palindrome program of String in Python

Using reverse and compare:

Below program is very simple. Here first we will reverse the original input string and then compare it with original input.

If both string, before reverse and after reverse is equal then print String is Palindrome otherwise not.

To reverse string in this program [:: -1] is used.

To compare string, == is used in if-else statement.

string = input("Please give a String : ")
if(string == string[:: - 1]):
   print("Given String is a Palindrome")
else:
   print("Given String is not a Palindrome") 

Output:

palindrome program in python

Using method and for loop

In this below program we have built some different logic to check string is palindrome or not.

Here using for loop we match each character. Here we will match first character with last character. Second character with second last character and so on..

Loop will be execute only half time of the length of string.

Here we have declared one method isPalindrome(string).

def isPalindrome(string): 
    for i in range(0, int(len(string)/2)): 
        if string[i] != string[len(string)-i-1]:
            return False
    return True
 
string = input("Please give a String : ")

if (isPalindrome(string)):
    print("Given String is a Palindrome")
else:
    print("Given String is not a Palindrome")
 
palindrome program in python