-1
def is_palindrome(input_string):
# We'll create two strings, to compare them
   if input_string[0:] == input_string[-1:]:
      return True
   else:
      return False

expected output is below:

print(is_palindrome("Never Odd or Even")) # Should be True print(is_palindrome("abc")) # Should be False print(is_palindrome("kayak")) # Should be True

I am currently getting False for all cases. I know my splicing might be off on the -1. I already tried swapping the colon around to no avail.

younggbon
  • 19
  • 1
  • 3
  • Please repeat [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). First, research the topic. Second, trace your own program's values. Either of these would have immediately shown your error. – Prune May 16 '20 at 20:57

2 Answers2

0

As you have it now, you're comparing all the letters to the last letter.

>>> a = [1,2,3]
>>> a[-1:]
[3]
>>> a[0:]
[1,2,3]

But that's not how you reverse a string, or any iterable. The step index should be negative:

>>> a = [1,2,3]
>>> a[::-1]
[3,2,1]

So you just want to compare the original string to that

David
  • 424
  • 3
  • 16
  • Thank you!. I guess I need to brush up on splicing indeed. This helped me get the last two test cases to pass.I think the first one is failing due to the spaces. I think I have to use the .join() method somehow. – younggbon May 16 '20 at 20:58
0

Also, I think you want to remove the spaces beforehand and lower or upper the letters.

l = ['Never Odd or Even', 'abc', 'kayak']

def isit(s):
    s = s.replace(' ', '').lower()
    return s == s[::-1]

for e in l:
    isit(e)


True
False
True
Touten
  • 190
  • 12