Python | Check if a string is a palindrome or not without Recursion

To solve this problem, we have round about 6 methods. Let us take a look at how all these methods are applied.

Method 1: Using string slicing

def palindrome_or_not(s):
   return s == s[::-1]
 
check_string = str(input("Enter the string for which you want to check whether it is palindrome or not :"))
print("The entered string is ", check_string)

if (palindrome_or_not(check_string)):
   print("The entered string is a palindrome")
else:
   print("The entered string isn't a palindrome")

Output:

Enter the string for which you want to check whether it is palindrome or not :paper
The entered string is paper
The entered string isn't a palindrome

Explanation:

We used the concept of string slicing. We reversed the string using negative indexing and compared that reversed string with the original string. If the value returned from the function is True then we can say that the string is a palindrome and if it is not then we can say that the string is not a palindrome.

Method 2: Using string slicing

def palindrome_or_not(s):
    x=list(s)
    y=[]
    y.extend(x)
    x.reverse()
    if(x==y):
        return True
    return False
 

check_string = str(input("Enter the string for which you want to check whether it is palindrome or not :"))
print("The entered string is ", check_string)

if (palindrome_or_not(check_string)):
   print("The entered string is a palindrome")
else:
   print("The entered string isn't a palindrome")

Explanation:

In this method, we first converted the string to a list using the list() function and then defined an empty list named y. Now, we used the extend function. All iterable (list, tuple, string, etc.) elements are added to the end of the list using the extend() function. And in the end, we used to reverse(). Python includes a built-in function called Python List reverse() that simply updates the original list rather than taking up additional space in order to reverse the items of the list. We then compared the two lists and returned values accordingly.

Method 3: Using break()

def palindrome_or_not(s):
    j = -1
    flag = 0
    for i in s:
        if i != s[j]:
            flag = 1
            break
        j = j - 1
    if (flag != 1):
        print("The entered string is a palindrome")
    else:
        print("The entered string isn't a palindrome")
 
check_string = str(input("Enter the string for which you want to check whether it is palindrome or not :"))
print("The entered string is ", check_string)

palindrome_or_not(check_string)

Method 4: Using the concept of assignment

def palindrome_or_not(s):
    w = ""
    for i in s:
        w = i + w

    if(s == w):
        print("The entered string is a palindrome")
    else:
        print("The entered string isn't a palindrome")


check_string = str(input(
    "Enter the string for which you want to check whether it is palindrome or not :"))
print("The entered string is ", check_string)

palindrome_or_not(check_string)

Output:

Enter the string for which you want to check whether it is palindrome or not :deified
The entered string is  deified
The entered string is a palindrome

Explanation

Here we accessed each element of the string that we passed on to the function. Now we will add that element ” i “ in the variable named ” w “. Once that is done, we compare both variables. If the strings stored inside them turn out to be the same then we say that, yes the entered string is a palindrome.

Method 5: Using reversed() function

def palindrome_or_not(s):
    rev = ''.join(reversed(s))

    if (s == rev):
        return True
    return False

check_string = str(input(
    "Enter the string for which you want to check whether it is palindrome or not :"))
print("The entered string is ", check_string)

result = palindrome_or_not(check_string)
if (result):
    print("The entered string is a palindrome")
else:
   print("The entered string isn't a palindrome")

Method 6: Breaking the string into half

def palindrome_or_not(s):
    for i in range(0, int(len(s)/2)):
        if s[i] != s[len(s)-i-1]:
            return False
    return True

check_string = str(input(
    "Enter the string for which you want to check whether it is palindrome or not :"))
print("The entered string is ", check_string)

result = palindrome_or_not(check_string)
if (result):
    print("The entered string is a palindrome")
else:
   print("The entered string isn't a palindrome")

Explanation:

This is one of the simplest approaches to solving this problem. Here we passed on our string and then divided the string by 2 and converted it to an integer. Now we compare the original string’s first character which is at index 0 to the last element of the string. This goes on till the defined range and finally, the decision of whether the string is palindrome or not is computed by passing True or False.

Here is the end of our article. We have defined 6 methods for you to learn different concepts of string slicing, break statement, and many more.

Share:

Author: Ayush Purawr