How to check for palindrome using Python logic

A pythonic way to determine if a given value is a palindrome:

str(n) == str(n)[::-1]

Explanation:

  • We’re checking if the string representation of n equals the inverted string representation of n
  • The [::-1] slice takes care of inverting the string
  • After that, we compare for equality using ==

Leave a Comment