Python program to check if a string is palindrome or not
Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome.
Examples:
Input : malayalam Output : Yes Input : geeks Output : No
Method #1
- Find reverse of string
- Check if reverse and original are same or not.
Python
# function which return reverse of a stringdef isPalindrome(s): return s == s[::-1]# Driver codes = "malayalam"ans = isPalindrome(s)if ans: print("Yes")else: print("No") |
Output :
Yes
Iterative Method: This method is contributed by Shariq Raza. Run a loop from starting to length/2 and check the first character to the last character of the string and second to second last one and so on …. If any character mismatches, the string wouldn’t be a palindrome.
Below is the implementation of above approach:
Python
# function to check string is# palindrome or notdef isPalindrome(str): # Run loop from 0 to len/2 for i in range(0, int(len(str)/2)): if str[i] != str[len(str)-i-1]: return False return True# main functions = "malayalam"ans = isPalindrome(s)if (ans): print("Yes")else: print("No") |
Output:
Yes
Method using the inbuilt function to reverse a string: This method is contributed by Shariq Raza. In this method, predefined function ‘ ‘.join(reversed(string)) is used to reverse string.
Below is the implementation of the above approach:
Python
# function to check string is# palindrome or notdef isPalindrome(s): # Using predefined function to # reverse to string print(s) rev = ''.join(reversed(s)) # Checking if both string are # equal or not if (s == rev): return True return False# main functions = "malayalam"ans = isPalindrome(s)if (ans): print("Yes")else: print("No") |
Output:
Yes
Method using one extra variable: In this method user take a character of string one by one and store in an empty variable. After storing all the character user will compare both the string and check whether it is palindrome or not.
Python
# Python program to check# if a string is palindrome# or notx = "malayalam"w = ""for i in x: w = i + wif (x == w): print("Yes")else: print("No") |
Output:
Yes
Method using flag: In this method user compare each character from starting and ending in a for loop and if the character does not match then it will change the status of the flag. Then it will check the status of flag and accordingly and print whether it is a palindrome or not.
Python
# Python program to check# if a string is palindrome# or notst = 'malayalam'j = -1flag = 0for i in st: if i != st[j]: j = j - 1 flag = 1 break j = j - 1if flag == 1: print("NO")else: print("Yes") |
Output:
Yes
Method using recursion:
This method compares the first and the last element of the string and gives the rest of the substring to a recursive call to itself.
Python3
# Recursive function to check if a# string is palindromedef isPalindrome(s): #to change it the string is similar case s = s.lower() # length of s l = len(s) # if length is less than 2 if l < 2: return True # If s[0] and s[l-1] are equal elif s[0] == s[l - 1]: # Call is pallindrome form substring(1,l-1) return isPalindrome(s[1: l - 1]) else: return False# Driver Codes = "MalaYaLam"ans = isPalindrome(s)if ans: print("Yes")else: print("No") |
Yes
This article is contributed by Sahil Rajput. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students



