Search code examples
pythontestingpalindrome

Check even/odd for Palindrome?


Is it a good idea to check for odd/even length of a palindrome number/string? Most snippets I came across don't do this basic test. If length is even, it can't be a palindrome, no?

if len(var) % 2 != 0:
  # could be a palindrome, continue...
else:
  break

Or is it just better (i.e faster) to start comparing the first and last numbers/letters directly?

Edit: Okay, stupid question, should've thought twice! :)


Solution

  • ABBA - an example of palindrome of four letters meaning even length.

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward...