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 reverse of the string is same as string. For example, “radar” is palindrome, but “radix” is not palindrome.
Examples:
Input : malayalam Output : Yes Input : geeks Output : No
Method #1
1) Find reverse of string
2) Check if reverse and original are same or not.
# function which return reverse of a string def reverse(s): return s[::-1] def isPalindrome(s): # Calling reverse function rev = reverse(s) # Checking if both string are equal or not if (s == rev): return True return False # Driver code s = "malayalam"ans = isPalindrome(s) if ans == 1: print("Yes") else: print("No") |
Output :
Yes
Iterative Method: This method is contributed by Shariq Raza. Run loop from starting to length/2 and check first character to last character of string and second to second last one and so on …. If any character mismatches, the string wouldn’t be palindrome.
Below is the implementation of above approach:
# function to check string is # palindrome or not def isPalindrome(str): # Run loop from 0 to len/2 for i in xrange(0, len(str)/2): if str[i] != str[len(str)-i-1]: return False return True # main function s = "malayalam"ans = isPalindrome(s) if (ans): print("Yes") else: print("No") |
Output:
Yes
Method using 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:
# function to check string is # palindrome or not def 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 function s = "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 a empty variable.After storing all the character user will compare both the string and check whether it is palindrome or not.
# Python program to check # if a string is palindrome # or not x = "malayalam"w = "" for i in x: w = i + w if (x==w): print("YES") |
Output:
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.
Recommended Posts:
- Python program to check if given string is vowel Palindrome
- C Program to Check if a Given String is Palindrome
- Java program to check whether a string is a Palindrome
- Python program to check if number is palindrome (one-liner)
- Python program to check if a given string is Keyword or not
- Python program to check if given string is pangram
- Python program to check if string is empty or not
- Python program to check if a string contains all unique characters
- Check if it is possible to create a palindrome string from given N
- Check if a given string is a rotation of a palindrome
- Check if any anagram of a string is palindrome or not
- Check if a string is palindrome in C using pointers
- Recursive function to check if a string is palindrome
- Check if there exists any sub-sequence in a string which is not palindrome
- Given two strings check which string makes a palindrome first
Improved By : mmishraofficial



