Python Programs to Check Whether a String is Palindrome or Not [5 Methods]

In this Python article, I will execute some Python programs to check whether a string is palindrome or not using methods like loops, string slicing, etc.

A Palindrome in Python is a string, number, or another sequence of units that can be read in the same way in either direction.

For Example: “REFER” if we read this from a forward direction or in a backward direction it remains the same.

In Python, you will find several methods for writing Python programs to check palindrome strings.

  • Using for-loop
  • Using a while loop
  • Using the reverse with the join function
  • Using the string-slicing
  • Using recursion

Python programs to check whether a string is palindrome or not using for loop

A for loop is used to iterate over strings. With the help of the if-else statement, I will check whether the string is palindrome or not in Python.

Here, is the complete program to check if a string is a palindrome or not using for loop in 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
word = "refer"
result = isPalindrome(word)
if (result):
        print("Yes")
else:
        print("No")

Here it will check only half of the string to determine if a string is a palindrome in Python.

 for i in range(0, int(len(str) / 2)):
                if str[i] != str[len(str) - i - 1]:

You can refer to the below screenshot after the Python code gets executed, it gives the output:

How to check if a string is a palindrome in Python

How to check if a given string is a palindrome in Python using a while loop

While loop is another way to iterate over the strings in Python. Here, I will use the ‘==’ equality operator to check whether the first character of the string is equal to the last character of the string and so on.

Here, is the full code of Python to check if the given string is a palindrome using the while loop.

def check_palindrome(string):
    string = string.lower()
    forward_increment = 0
    backward_decrement = len(string) - 1
    while forward_increment < backward_decrement:
        if string[forward_increment] == string[backward_decrement]:
              return True
        forward_increment += 1
        backward_backword -= 1
    return True

input_string = input("Enter a string: ")
if check_palindrome(input_string):
    print("The string is a palindrome.")
else:
    print("The string is not a palindrome.")

Output: Refers to the below-presented image to see the output after executing the Python program.

Python program to check whether a string is palindrome or not using while loop

Python program to check whether a string is Palindrome or not using the reverse and join function

In this case in Python, I will use the built-in reverse() method to iterate the characters of the strings in a reversed order. Also, I will use the join method to join the reversed characters.

This is the complete code of Python to check whether a string is a palindrome using reverse with join function.

def is_Palindrome(string): 
    reversed_string =''.join(reversed(string)) 
    if string==reversed_string: 
        return "The string is a palindrome." 
    return "The string is not a palindrome." 
string = input ("Enter string: ") 
print(is_Palindrome(string))

Output: The below screenshot shows the output after the code gets executed in Python.

Python Program to Check Whether a String is Palindrome

Python program to check whether a string is a palindrome using the string-slicing

If you don’t want to use the reverse and join method you can use the string slicing method to check whether a string is a palindrome in Python.

The slicing operation in Python is used to create a reversed copy of a string. This is the [: : -1] notation, I will use for effectively reversing the strings.

Here, is the complete code to check whether a string is a palindrome using string slicing in Python.

string = input("Enter string: ") 
if(string == string[::-1]) : 
    print("The string is a palindrome.") 
else: 
    print("The string is not a palindrome.")

Output: You can see below the output after the Python code gets executed to check if a string is a palindrome or not.

Palindrome program in Python using slicing

Python program to check if the given string is a palindrome using recursion

In Python, the recursion method is the process where the Python function calls itself inside it.

Here, is the full program to check if the string is a palindrome using recursion in Python.

def check_for_Palindrome(string): 
    if len(string) < 1: 
        return True 
    else: 
        if string[0] == string[-1]: 
            return check_for_Palindrome(string[1:-1]) 
        else: 
            return False 
input_word = input("Enter string : ") 
 
if(check_for_Palindrome(input_word)==True): 
    print("The string is a palindrome.") 
else: 
    print("The string is not a palindrome.")

Here, I have used the different conditional statements to compare the first and last characters of a string in Python.

if string[0] == string[-1]: 
            return check_for_Palindrome(string[1:-1]) 

Now, if the first and last characters of a string are the same, then the function calls itself recursively.

Output: Refers to the below image to see the output after executing the Python program to check whether the string is a palindrome or not.

Python Programs to Check Whether a String is Palindrome or Not using recursion

Conclusion

I hope this Python tutorial, provides useful information and gives a clearer picture of how to check if a string is a palindrome in Python. I have explained what is a palindrome as well.

Moreover, I have explained different methods to write some Python programs to check whether a string is palindrome or not such as by using for loop, while loop, reverse with the join function, string slicing, and recursion with some examples.

You may also like to read: