Is there any palindrome function in python?

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 

  1. Find reverse of string
  2. Check if reverse and original are same or not.

Python

def isPalindrome(s):

    return s == s[::-1]

s = "malayalam"

ans = isPalindrome(s)

if ans:

    print("Yes")

else:

    print("No")

Output : 

Yes

Time complexity: O(n)

Auxiliary Space: O(1)

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

def isPalindrome(str):

    for i in range(0, int(len(str)/2)):

        if str[i] != str[len(str)-i-1]:

            return False

    return True

s = "malayalam"

ans = isPalindrome(s)

if (ans):

    print("Yes")

else:

    print("No")

Output: 

Yes

Time complexity: O(n)

Auxiliary Space: O(1)

Method using the inbuilt function to reverse a string: 

In this method, predefined function ‘ ‘.join(reversed(string)) is used to reverse string. 

Below is the implementation of the above approach: 

Python

def isPalindrome(s):

    rev = ''.join(reversed(s))

    if (s == rev):

        return True

    return False

s = "malayalam"

ans = isPalindrome(s)

if (ans):

    print("Yes")

else:

    print("No")

Output: 

Yes

Time complexity: O(n)

Auxiliary Space: O(n)

Method using one extra variable: In this method, the user takes a character of string one by one and store it in an empty variable. After storing all the characters user will compare both the string and check whether it is palindrome or not. 

Python

x = "malayalam"

w = ""

for i in x:

    w = i + w

if (x == w):

    print("Yes")

else:

    print("No")

Output: 

Yes

Time complexity: O(n)

Auxiliary Space: O(n)

Method using flag: In this method, the user compares 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 the flag and accordingly and print whether it is a palindrome or not.  

Python

st = 'malayalam'

j = -1

flag = 0

for i in st:

    if i != st[j]:

        flag = 1

        break

    j = j - 1

if flag == 1:

    print("NO")

else:

    print("Yes")

Output: 

Yes

Time complexity: O(n)

Auxiliary Space: O(1)

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

def isPalindrome(s):

    s = s.lower()

    l = len(s)

    if l < 2:

        return True

    elif s[0] == s[l - 1]:

        return isPalindrome(s[1: l - 1])

    else:

        return False

s = "MalaYaLam"

ans = isPalindrome(s)

if ans:

    print("Yes")

else:

    print("No")

Output:

Yes

Time complexity: O(n)

Auxiliary Space: O(n)

Method : Using extend() and reverse() methods

Python3

def isPalindrome(s):

    x=list(s)

    y=[]

    y.extend(x)

    x.reverse()

    if(x==y):

        return True

    return False

s = "malayalam"

ans = isPalindrome(s)

if ans:

    print("Yes")

else:

    print("No")

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . 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.


What is palindrome function in Python?

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.

Is palindrome possible program in Python?

n=int(input("Enter number:")) temp=n rev=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 if(temp==rev): print("The number is a palindrome!") else: print("The number isn't a palindrome!") 1. User must first enter the value of the integer and store it in a variable.

How do you make a palindrome in Python?

Palindrome Program.
str = 'JaVaJ'.
strstr = str.casefold().
# This string is reverse..
rev = reversed(str).
if list(str) == list(rev):.
print("PALINDROME !").
print("NOT PALINDROME !").

How do you check if a text is a palindrome in Python?

casefold() # reverse the string rev_str = reversed(my_str) # check if the string is equal to its reverse if list(my_str) == list(rev_str): print("The string is a palindrome.") else: print("The string is not a palindrome.")