Table of Contents

Python code to check if a given String is Palindrome

 

Python code implementation without user-defined functions & classes

Code: 
#Python code to Check if a given String is Palindrome



a = input("Enter the String: ")
len_a = len(a)
y = int(len_a/2)


if a[0:y] == a[-1:-y-1:-1]:
    print(a, " : is a Palindrome String")
else:
    print(a, " : is not a Palindrome String")
Executing Python Script: 
Enter the String: malayalam
malayalam  : is a Palindrome String

 

Output: 
malayalam : is a Palindrome String

 

 

Python code implementation using function

Code: 
#Python code to Check if a given String is Palindrome using function

def palindrome_func(a_string):
    len_a = len(a_string)
    y = int(len_a / 2)

    if a_string[0:y] == a_string[-1:-y - 1:-1]:
        print(a_string, " : is a Palindrome String")
    else:
        print(a_string, " : is not a Palindrome String")


a = input("Enter the String: ")
palindrome_func(a)

 

Executing Python Script: 
Enter the String: malayalam
malayalam  : is a Palindrome String

 

Output: 
malayalam : is a Palindrome String

 

 

Python code implementation using Classes

Code: 
#Python code to Check if a given String is Palindrome using class

class Palindrome_class(object):
    def palindrome_func(self, a_string):
        self.a_string = a_string
        len_a = len(self.a_string)
        y = int(len_a / 2)

        if self.a_string[0:y] == self.a_string[-1:-y - 1:-1]:
            print(self.a_string, " : is a Palindrome String")
        else:
            print(self.a_string, " : is not a Palindrome String")


a = input("Enter the String: ")
# Creating the Object
Object_1 = Palindrome_class()

# Calling Function using created Object
Object_1.palindrome_func(a)

Executing Python Script: 
Enter the String: malayalam
malayalam  : is a Palindrome String

 

Output: 
malayalam : is a Palindrome String

 

 

 

 

Enjoy Python Code By Pythonbaba 🙂