Python Program to Check if a String is Palindrome or Not
The task of checking if a string is a palindrome in Python involves determining whether a string reads the same forward as it does backward. For example, the string “madam” is a palindrome because it is identical when reversed, whereas “hello” is not.
Using two pointer technique
This approach involves using two pointers, one starting from the beginning and the other from the end of the string, moving towards the center while comparing characters. It is the most efficient because it checks only half the string and requires constant space.
s = "malayalam" # string
i,j = 0, len(s) - 1 # two pointers
is_palindrome = True # assume palindrome
while i < j:
if s[i] != s[j]: # mismatch found
is_palindrome = False
break
i += 1
j -= 1
if is_palindrome:
print("Yes")
else:
print("No")
Output
Yes
Explanation: while loop compares characters from both ends towards the center as long as i < j . If a mismatch is found, is_palindrome is set to False and the loop breaks otherwise, the pointers move inward. After the loop, it prints “Yes” if is_palindrome is True, otherwise “No”.
Table of Content
Using slicing method
A simple and elegant method using Python’s slicing feature (s[::-1]) to reverse the string and compare it with the original. It is highly readable but creates an extra copy of the string, leading to additional space usage.
s = "malayalam" # string
if s == s[::-1]:
print("Yes")
else:
print("No")
Output
Yes
Explanation: It compares the string s with its reverse s[::-1]. If they are equal, it prints “Yes” otherwise, it prints “No”.
Using reversed()
reversed() function combined with join() to form a reversed string and check equality with the original. It is clean and readable but requires extra memory to store the reversed string.
s = "geeks" # string
rev = ''.join(reversed(s))
if s == rev:
print("Yes")
else:
print("No")
Output
No
Explanation: It creates a reversed version of the string with ”.join(reversed(s)) and compares it with the original string. If they are equal, it prints “Yes” otherwise, it prints “No”.
Using recursion
A recursive approach that compares the first and last characters while reducing the problem to a smaller substring. Though it works logically, it is less efficient due to function call overhead and stack space consumption.
def is_palindrome(s,i,j):
if i >= j: # base case
return True
if s[i] != s[j]: # mismatch found
return False
return is_palindrome(s, i + 1, j - 1)
s = "geeks" # string
if is_palindrome(s, 0, len(s) - 1): # call function with initial pointers
print("Yes")
else:
print("No")
Output
No
Explanation: is_palindrome(s, i, j) checks if a string is a palindrome by using two pointers, i (left) and j (right). If i >= j, it returns True as all characters matched. If s[i] != s[j], it returns False due to a mismatch. Otherwise, it recursively checks the inner substring with i + 1 and j – 1.
Using for loop
This naive approach builds the reversed string character by character using a loop and concatenation. However, string concatenation in Python is costly, making this the least efficient method, especially for large strings.
s = "malayalam" # string
rev = "" # to store the reversed string
for char in s:
rev = char + rev
if s == rev:
print("Yes")
else:
print("No")
Output
Yes
Explanation: for loop iterates through each character in s, prepending it to rev to build the reversed string. After the loop, s is compared with rev. If they are equal, “Yes” is printed, otherwise “No”.