Check string is Palindrome or not- Python

Palindrome in python

Problem is to check whether a given string is palindrome or not. If the given string is palindrome 

Return True and if not return False. 

Constraints:  

Given string can be of multiple words. 

For Example: 

Intput:“never odd or even”  

Output:

True 

Input: “madam” 

Output:

True 

Input: “abab” 

Output:

False 

Code:

#Check string is Palindrome or not

def strChallenge(str2):

    str1=str2.replace(" ","")
    return str1==str1[::-1]

print(strChallenge("never odd or even"))

#Check string is Palindrome or not

Explanation:

Here first we will remove the spaces between words.

Then we will match using the negative indexing in python with the new string.

You can refer the Python indexing documentation

Leave a Comment