Skip to content
scipython.com

Q2.3.2Determine if a string is a palindrome

Write a single-line expression for determining if a string is a palindrome (reads the same forwards as backwards).

Show solution

Simply slice the string backwards and compare with the original:

>>> s = 'banana'
>>> s == s[::-1]
False
>>> s = 'deified'
>>> s == s[::-1]
True