Select Page

# Program to check the given string is palindrome or symmetrical using Python

by | Apr 1, 2021 | Python Programs

Home » Python » Python Programs » Program to check the given string is palindrome or symmetrical using Python

## Introduction

In the previous section, we learnt how to check whether the string is palindrome or not. Now we will look at how to check whether the string is symmetrical.

A string is said to be symmetrical if both the halves of the given string are same whereas, a string is said to be palindrome if the one half of the string is the reverse of another half.

## Program

```input_str = "civic"

def isPalindrome_reversed(input_str):
rev_str = ''.join(reversed(input_str))
if (input_str == rev_str):
return True
return False

def symmetry_string(s):
length = len(s)
flag = 0
if length % 2:
mid = length//2 + 1
else:
mid = length//2
s_1 = 0
s_2 = mid
while(s_1 < mid and s_2 < length):
if(s[s_1] == s[s_2]):
s_1 = s_1 + 1
s_2 = s_2 + 1
else:
flag = 1
break
if flag == 0:
print("Yhe string {0}, is symmetrical!".format(input_str))
else:
print("The string {0}, is not symmetrical!".format(input_str))
result2 = isPalindrome_reversed(input_str)
if (result2):
print("The string {0}, is palindrome!".format(input_str))
else:
print("The string {0}, is not palindrome!".format(input_str))

symmetry_string(input_str)```

## Explanation

In the above python code, we have checked whether the string is palindrome or symmetrical. Considering the property of palindrome and symmetry for string we have created two functions: isPalindrome_string and symmetry_string. For checking palindrome we reversed the string using in-built function ‘ ‘.join( reversed() ) and checked if its equalled to the input string. If the condition satisfies the input string is palindrome otherwise not.

For checking whether the string is symmetrical, first we split the string into two half’s. If the string length is even, then it is split into two equal length strings whereas, if the length of input string is odd, the middle element will be untouched for checking condition. Secondly, we are checking the characters of the halves. If both the halves are same, the input string is symmetrical otherwise not.

# 1 Comment

1. def inverse(a):
a_new=”
for i in range(1,len(a)+1):
a_new+=a[-i]
return a_new
a=(input())
if a==inverse(a):
print(‘its symmetic’)
else:
print(‘its not symmetric’)