Program to check if the given string is palindrome using Python

Introduction

In this section of python programming, we will check whether the given string is palindrome or not. In general meaning, a palindrome is a word, or phrase which reads exactly same backward or forward.

Program to check if the given string is palindrome using Python

Program

input_str = 'civic'
def isPalindrome_slicing(i_str):
    return i_str == i_str[::-1]
   
result = isPalindrome_slicing(input_str)
print("Result on using slicing to reverse the string: ")
if result:
    print("The given string is palindrome! \n")
else:
    print("The given string is not palindrome! \n")
   
def isPalindrome_reversed(input_str):
    rev_str = ''.join(reversed(input_str))
    if (input_str == rev_str):
        return True
    return False
   
result2 = isPalindrome_reversed(input_str)
print("Result on using in-built function:")
if result2:
    print("The given string in palindrome!")
else:
    print("The given string is not palindrome!")

 

Output

Program to check if the given string is palindrome Output

Explanation

In the above python code, we have reversed the given string and checked against the given word. If the reverse is equalled to the given word, then the string is palindrome else it is not.

To reverse the string we have used two ways, one way is to reverse using the slicing method and another way is by using in-built function  ‘ ‘ .join( reversed() )

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.