JDF JDF - 1 year ago 120
Python Question

Creating palindrome function that returns boolean values in Python

I am currently taking a course on Python and am currently struggling with one portion of my homework.

The question is asking us to construct a function that checks a string to see if it is a palindrome. The problem I am having is that for one of the tests my instructor has provided is for the palindrome "Never odd or even" which contains spaces. The spaces are causing my function to fail because it won't just use the letters in the phrase.

My current code is:

def is_palindrome(phrase):

return phrase == phrase[::-1]


She is testing with the code:

assert is_palindrome("Never odd or even")


Any help would be appreciated! Thanks.

Answer Source

I think this is what you want:-
is_palindrome("Never odd or even".replace(" ", "").lower())

Or

If you want to change your function then your function look like:

def is_palindrome(phrase):
    phrase=phrase.replace(" ","").lower()
    return phrase == phrase[::-1]   

and you can call it using is_palindrome("Never odd or even")

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download