Signup/Sign In

Python program to check if a string is palindrome or not

In this tutorial, you will learn to check if a string is palindrome or not in Python. Strings in Python are a sequence of characters stored between quotes (" "). A string is said to be palindrome if a string is read the same way as its reverse. For example- madam is a palindrome as the reverse of this string is also madam. For a given string our program should check and return Yes if the string is a palindrome and No if the string is not a palindrome. For example,

Input: cricket

Output: No

Input: level

Output: Yes

The simple approach to execute this program will be to first find the reverse of the string and then compare the reverse with the string. In Python, there are several other approaches that we can follow:

  1. Reverse string and compare
  2. The iterative method of checking each character in the string
  3. By using built-in reversed() function

Approach 1: Reverse string and compare

For this approach, we will first reverse the string and then compare the reversed string with the original string to see if it is palindrome or not. To reverse the string we will be using the concept of slicing. We will be using if conditional statement to compare the reverse and the original string.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept the string and check if it is a palindrome

Step 2- Use if condition to check that the reverse is the same as the string

Step 3- If the condition is satisfied return Yes

Step 4- Else return No

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. We will store the reversed string in another variable and compare that variable to the original string and return Yes or No accordingly. To get the reverse we have used the slicing concept and used negative indexing to access the characters in reverse.

def isPalindrome(string):
    if string==string[::-1]:
        return "Yes"
    else:
        return "No"

s1="LEVEL"
s2="Hello"
print(s1,":",isPalindrome(s1))
print(s2,":",isPalindrome(s2))


LEVEL : Yes
Hello : No

Approach 2: iterative method

In this approach, we will compare the first character with the last character, the second character with the second last character of the string and so on... and check if they are the same or not. If the program will encounter any mismatch then the string is not a palindrome.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept the string and check if it is a palindrome

Step 2- In the function run a loop from 0 to the middle of the string s

Step 3- Check character in the ith position from the start and end

Step 4- If a character is found which is not same then return "No"

Step 5- If no character is found return "Yes"

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. To get the middle index of the string we will use the len() function which will return the size of the string.

# palindrome string
# iterative method
# function
def isPalindrome(string):
    for i in range(int(len(string)/2)):
        if string[i] != string[len(string)-i-1]:
            return "No"
    return "Yes"

s1="tenet"
s2="defined"
print(s1,":",isPalindrome(s1))
print(s2,":",isPalindrome(s2)


tenet : Yes
defined : No

Approach 3: using reversed() function

In this approach, we will use the built-in reversed() function which returns an iterator that accesses the given sequence in the reverse order then we will add the characters in reverse order to a new string. Then we will compare the new string with the original string.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept the string and check if it is a palindrome

Step 2- In the function declare a string that will store the reversed string

Step 3- Check if the reversed string is equal to the original string

Step 4- If the condition is satisfied then return "Yes"

Step 5- Else return"No"

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach. To add the characters in reverse order in a new string we will use the join() function.

def isPalindrome(string):
    rev=''.join(reversed(string))
    if string==rev:
        return "Yes"
    return "No"   

s1="refer"
s2="program"
print(s1,":",isPalindrome(s1))
print(s2,":",isPalindrome(s2))


refer : Yes
program : No

Conclusion

In this tutorial, we have discussed the three ways for checking if a string is palindrome or not. We have discussed the simple approach of reversing a string and then checking it. We have also discussed the iterative approach and how to use reversed() function to check palindrome.