Given a string. the task is to check if the string is symmetrical and palindrome or not. A string is said to be symmetrical if both the halves of the string are the same and a string is said to be a palindrome string if one half of the string is the reverse of the other half or if a string appears same when read forward or backward.
Example:
Input: khokho Output: The entered string is symmetrical The entered string is not palindrome Input:amaama Output: The entered string is symmetrical The entered string is palindrome
Approach: The approach is very naive. In the case of palindrome, a loop is run to the mid of the string and the first and last character are matched. If the characters are not similar then the loop breaks and the string is not palindrome otherwise the string is a palindrome. In the case of symmetry, if the string length is even then the string is broken into two halves and the loop is run, checking the characters of the strings of both the half. If the characters are not similar then the loops break and the string is not symmetrical otherwise the string is symmetrical. If the string length is odd then the string is broken into two halves in such a way that the middle element is left unchecked, and the above same step is repeated.
Below is the implementation.
# Python program to demonstrate # symmetry and palindrome of the # string # Function to check whether the # string is plaindrome or not def palindrome(a): # finding the mid, start # and last index of the string mid = (len(a)-1)//2 start = 0 last = len(a)-1 flag = 0 # A loop till the mid of the # string while(start<mid): # comparing letters from right # from the letters from left if (a[start]== a[last]): start += 1 last -= 1 else: flag = 1 break; # Checking the flag variable to # check if the string is palindrome # or not if flag == 0: print("The entered string is palindrome") else: print("The entered string is not palindrome") # Function to check whether the # string is symmetrical or not def symmetry(a): n = len(a) flag = 0 # Check if the string's length # is odd or even if n%2: mid = n//2 +1 else: mid = n//2 start1 = 0 start2 = mid while(start1 < mid and start2 < n): if (a[start1]== a[start2]): start1 = start1 + 1 start2 = start2 + 1 else: flag = 1 break # Checking the flag variable to # check if the string is symmetrical # or not if flag == 0: print("The entered string is symmetrical") else: print("The entered string is not symmetrical") # Driver code string = 'amaama'palindrome(string) symmetry(string) |
Output:
The entered string is palindrome The entered string is symmetrical
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python program to check if a string is palindrome or not
- Python program to check if given string is vowel Palindrome
- Python program to check if number is palindrome (one-liner)
- Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Python Program to Check Whether a Number is Positive or Negative or zero
- Python program to check whether the values of a dictionary are in same order as in a list
- Python program to check whether a number is Prime or not
- Python Set | Check whether a given string is Heterogram or not
- Python | Check whether a string is valid json or not
- Python | Check whether string contains only numbers or not
- Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
- Check if Binary representation is Palindrome in Python
- Python | Check whether two lists are circularly identical
- Python | Check whether given key already exists in a dictionary
- Python regex | Check whether the input is Floating point number or not
- Python | Check whether two lists follow same pattern or not
- Python - Check whether the given List forms Contiguous Distinct Sub-Array or Not
- Python PRAW – Check whether a redditor is friends with the user
- Python PRAW – Check whether a redditor has Reddit premium or not
- How to check whether the day is a weekday or not using Pandas in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

