palindrome iteration

Jussi Piitulainen jpiitula at
Fri Aug 27 21:43:10 CEST 2010

Dave Angel writes:

> Jussi Piitulainen wrote:
>> Ian writes:
>>> Of course, the simpler way is to use the definition of a
>>> Palindrome as the same backwards and forwards.
>>> def isPalindrome(pal)
>>>      return pal == pal.reverse
>> Agreed. But is there any nicer way to spell .reverse than [::-1] in
>> Python? There is .swapcase() but no .reverse(), right?
> There can't be a .reverse() method on string, because it's
> immutable. You could use
>     "".join(reversed(pal))
> but I'd prefer  pal[::-1]  as I said earlier.

There could easily be a .reverse() method on strings. It would return
the reversed string, like .swapcase() returns the swapcased string.

More information about the Python-list mailing list