Python reverse() for palindromes
Try y = x[::-1]
. This uses splicing to get the reverse of the string.
reversed(x)
returns an iterator for looping over the characters in the string in reverse order, not a string you can directly compare to x
.
reversed
returns an iterator, which you can make into a string using the join
method:
y = ''.join(reversed(x))
For future reference, a lambda from the answers above for quick palindrome check:
isPali = lambda num: str(num) == str(num)[::-1]
example use:
isPali(9009) #returns True