Check Whether String is Palindrome or Symmetrical in Python
In this tutorial, we will check whether a given string is Palindrome or Symmetrical using the Python language. Before doing this task, we need to know about a string in Python. Lets first recap on
A string in Python is nothing but a sequence of characters enclosed within single or double-quotes. Here, a special feature of Python strings is that they are immutable,i.e they cannot change once defined. The main advantage of string is that Python supports in-built functions to manipulate strings the way we want. These functions don’t change the strings; they just modify them.
Now, let’s know more about Palindrome and Symmetrical strings, before proceeding further deep.
What are Palindrome Strings?
First, we will explore the concept of Palindrome!
Palindrome represents a sequence of characters that spell the same both forward as well as backward. This means that when the sequence is reversed; the characters stay the same as the original sequence of characters. A palindrome is of three different types :
- Palindrome Numbers
- Palindrome Strings
- Palindrome Phrase
We will need a Palindrome String here. Palindrome Strings are a sequence of alphabets which when reversed, stay similar to the original sequence. Let us take an example :
Input : madam Output : This string is palindrome Input : similar Output : This string is not palindrome
If we see properly, the string ‘madam‘ when reversed, it reads as ‘madam‘. Hence, this string is a palindrome.
What are Symmetrical Strings?
A string that, when broken into two halves, produces two similar sequences of characters is called a symmetrical string. That is, the division occurs in the middle. Here is an example :
Input : yoyo Output : The given string is symmetrical Input : madam Output : The given string is not symmetrical
Here, we can see that the string ‘yoyo‘ when broken into 2 halves, it gives the portions ‘yo‘ and ‘yo‘ which are the same. Hence, this string is symmetrical.
Approach to the Given Problem
- First, in the main code, take string as input and call the palin() function first and pass string as argument.
Palin() function :
- Firstly, initialize st and end and f with the starting and ending index of string and 0 respectively.
- Next, run while loop to traverse st from start towards last and end from last towards start of string with condition st<end.
- Within the while loop,run an if statement comparing the characters in the index positions st and end. If comparison holds ,i.e, all the characters spelled forward and backward are same, f will remain 0 else f will become 1 and break the loop.
- Lastly, we run another if statement to check value of f; if f=0 then comparison is true hence we will print ‘palindrome string’ else we will print ‘not palindrome’.
- Function ends here and passes back control to main function.
Symm() function :
- From main function, we call the symm() function and pass string as argument.
- Initialize flag with 0.
- Next, run an if statement to check whether string length is even or odd and calculate mid-value accordingly. If even, the mid-value will be the mid index else, will be mid index+1.
- Initialize starting index of first and second portion of string, s1 and s2 with 0 and mid-value.
- Next, run a while loop to traverse from startings s1 and s2 in string provided s1<mid and s2< length of string.
- Within the loop,run an if statement to compare the characters in the index positions s1 and s2. If all the comparisons,i.e, string[s1] and string[s2] till the range given are true; then flag will remain 0 else, flag will become 1 and instantly break the loop.
- Lastly, if flag is 0 then we will print ‘symmetrical string’ ,else we will print ‘not symmetrical string’.
# palin function to check whether string palindrome or not def palin(string): # declare and initialize with the starting and ending indexes st = 0 end = len(string)-1 f = 0 # loop comparing letters moving from start to end and from end to start while(st<end): if (string[st]== string[end]): st += 1 end -= 1 else: f = 1 break; # if loop with f as condition if f == 0: print("The entered string is palindrome") else: print("The entered string is not palindrome") # symm function to check string symmetrical or not def symm(string): l = len(string) flag = 0 # to check length of string even or odd # to calculate middle value accordingly if l%2 == 0: mid = l//2 # for even length else: mid = l//2 + 1 # for odd length s1 = 0 # starting for first portion of string s2 = mid # starting for rest portion of string after middle value while(s1 < mid and s2 < l): if (string[s1] == string[s2]): # comparing from start of both portions # of given string s1 = s1 + 1 s2 = s2 + 1 else: flag = 1 break if flag == 0: print("The entered string is symmetrical") else: print("The entered string is not symmetrical") # Main code string = input("Enter the string: ") palin(string) symm(string)
Output : Enter the string: malayalam The entered string is palindrome The entered string is not symmetrical
Here, we took the string as ‘malayalam‘ . We can see that the first 4 words are ‘mala‘ and the last 4 words spelled backward is also ‘mala‘. When the loop of palin() function begins, the first letter ‘m‘ is compared with the last letter ‘m‘, then the second letter with the second last letter, and so on. And we find out that all the comparisons are true. Hence, ‘malayalam‘ is a palindrome string.
On the other hand, for symm() function, we see that ‘mala‘ and ‘yalam‘ are different strings. Hence proved not symmetrical and we get the output.
Output : Enter the string: momo The entered string is not palindrome The entered string is symmetrical
Here, we took the string ‘momo‘. In palin() function, we can see that ‘mo‘ and ‘om‘ are 2 different sequences of characters and so it is not a palindrome.
In symm() function, when we break ‘momo‘ into ‘mo‘ and ‘mo‘ we see that they are similar. Hence proved that ‘momo’ is symmetrical and we get the output.
Thank you for going through this article. I sincerely hope this article was of some help to the readers and was able to solve doubts related to this topic. Also do check out the related articles below :