invert or reverse a string... warning this is a rant

Paul Rubin http
Thu Oct 19 12:09:49 EDT 2006


rick <ath-admin at vt.edu> writes:
> Why can't Python have a reverse() function/method like Ruby?
> 
> Python:
> x = 'a_string'
> # Reverse the string
> print x[::-1]
> 
> The Ruby approach makes sense to me as a human being. The Python
> approach is not easy for me (as a human being) to remember. Can that
> be changed or should I just start blindly memorizing this stuff?

You could use:

   print ''.join(reversed(x))

That also looks a little bit weird, but it combines well-known Python
idioms straightforwardly.



More information about the Python-list mailing list