palindrome iteration

Jussi Piitulainen jpiitula at
Sat Aug 28 14:11:03 CEST 2010

Steven D'Aprano writes:
> On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote:
>> Terry Reedy writes:
>>> On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:
>>> > Dave Angel writes:
>>> > There could easily be a .reverse() method on strings. It would return
>>> > the reversed string, like .swapcase() returns the swapcased string.
>>> Could be, but the main use case seems to be for palindrome testing ;-)
>>> Given that slicing and reversed() can do the same thing, the need is
>>> thin.
>> The need is quite thin, but immutability of strings is not an issue,
>> just like there can be .swapcase() though strings are immutable. That is
>> all I am saying above.
> You're right, there could be a reversed() method for strings. There
> could also be a disemvowel method that removes vowels, a randomise
> method that shuffles the letters around, a studlycaps method that
> changes the case of each letter randomly, and a method to check that
> brackets () are well- formed. They would all be useful to
> somebody. There are lots of different methods that strings could
> have. Where do you draw the line?

When I said that there could be such a method, I was merely objecting
to a statement, made in response to me, that there could not be such a
method because strings are immutable. You clearly agree with me that
that statement was not correct. Would you have let it stand if it was
made to you?

To answer your question, I don't see a real need for .reversed() in
strings, but I do think .reversed() would be much more useful than
.swapcase() which is in Python now and for which I see no use at all.

I have not proposed adding anything to Python. I have only asked if
there is any nicer expression for string reversal than [::-1] in
Python now, and corrected an incorrect statement that was made in
response to me that there could not be a string reversal method
because Python strings are immutable.

I am still not proposing that anything be added to Python.

I have not even criticized Python for not having a nicer expression
for string reversal than [::-1]. I have merely asked if there is one,
because I didn't know if there is one, and I have shown some snippets
of code to illustrate what I might mean by nicer. Someone even
understood me. (Thanks.)

I think I have received the answer to my question by now - that there
is no obviously nicer way, and all other string reversal expressions
require some extra cruft and overhead.

> Not everything needs to be a built-in method. There is already a
> standard way to spell "reverse a string":
> astring[::-1]
> If you don't like that, you can do this:
> ''.join(reversed(astring))

I know. I agree. I was also shown a different way to test for

list(s) == list(reversed(s))

which is quite nice apart from the overhead.

> I don't object to a hypothetical reverse() method on strings, but
> the gain is minimal.

I have not suggested that such a method should be added to the
language. I merely corrected a statement that there could not be such
a method because strings are immutable. I would not have bothered to
do even that if that incorrect statement had not been made in response
to my own post.

I agree that the gain would be minimal. There is no harm in the method
either, so I would not object to it if somebody were to propose its
addition, but just to clarify my position: I have not proposed it.

Hope this helps.

More information about the Python-list mailing list