16 Nov 2009

Python Tips - String Reverse

how to reverse a string using python?
How can u do it?
How many ways can u do it?
How clear is ur method?

Just have a look:



1, slice operation
>>> s = "abcdefg"
>>> s[::-1]


2, reduce function
>>> reduce(lambda x,y:y+x,s)


3, use list
>>> a=list(s)
>>> a.reverse()
>>> "".join(a)

No comments :

Post a Comment