Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to check whether the string is Symmetrical or Palindrome
When checking if a string is symmetrical or palindromic, we need to understand two different concepts. A palindrome reads the same forwards and backwards (like "racecar"), while a symmetrical string has identical first and second halves (like "abcabc").
Understanding Palindromes vs Symmetrical Strings
A palindrome compares characters from both ends moving inward, while symmetry compares the first half with the second half of the string.
Complete Example
Here's a program that demonstrates both palindrome and symmetry checking ?
def check_palindrome(my_str):
mid_val = (len(my_str)-1)//2
start = 0
end = len(my_str)-1
flag = 0
while(start < mid_val):
if (my_str[start] == my_str[end]):
start += 1
end -= 1
else:
flag = 1
break
if flag == 0:
print("The entered string is palindrome")
else:
print("The entered string is not palindrome")
def check_symmetry(my_str):
n = len(my_str)
flag = 0
if n % 2:
mid_val = n//2 + 1
else:
mid_val = n//2
start_1 = 0
start_2 = mid_val
while(start_1 < mid_val and start_2 < n):
if (my_str[start_1] == my_str[start_2]):
start_1 = start_1 + 1
start_2 = start_2 + 1
else:
flag = 1
break
if flag == 0:
print("The entered string is symmetrical")
else:
print("The entered string is not symmetrical")
# Test with different strings
my_string = 'phphhphp'
print("Testing string:", my_string)
print("The method to check a palindrome is being called...")
check_palindrome(my_string)
print("The method to check symmetry is being called...")
check_symmetry(my_string)
print("\nTesting with 'racecar' (palindrome):")
check_palindrome('racecar')
check_symmetry('racecar')
print("\nTesting with 'abcabc' (symmetrical):")
check_palindrome('abcabc')
check_symmetry('abcabc')
Testing string: phphhphp The method to check a palindrome is being called... The entered string is palindrome The method to check symmetry is being called... The entered string is not symmetrical Testing with 'racecar' (palindrome): The entered string is palindrome The entered string is not symmetrical Testing with 'abcabc' (symmetrical): The entered string is not palindrome The entered string is symmetrical
How the Functions Work
Palindrome Check: Compares characters from start and end positions moving toward the center. If all pairs match, it's a palindrome.
Symmetry Check: Divides the string into two halves and compares corresponding positions. For odd-length strings, the middle character is ignored.
Key Differences
| Property | Palindrome | Symmetrical |
|---|---|---|
| Comparison | Start ? End positions | First half ? Second half |
| Example | "racecar" | "abcabc" |
| Reading | Same forwards/backwards | Identical halves |
Conclusion
Palindromes read the same in both directions, while symmetrical strings have matching first and second halves. Both use similar loop logic but with different comparison strategies.
