palindrome iteration

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Aug 28 06:55:30 EDT 2010


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?

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 don't object to a hypothetical reverse() method on strings, but the 
gain is minimal.



-- 
Steven



More information about the Python-list mailing list