Python的字符串反转方法

Published: Tags: PYTHON

假设有字符串”s”,如下方法进行反转(下面是在iPython中的输入输出):

In [1]: s='123456789' In [2]: s Out[2]: ‘123456789’

In [3]: s[::-1] Out[3]: ‘987654321’

In [4]: reduce(lambda x,y:y+x,s) Out[4]: ‘987654321’

In [5]: x=list(s); x.reverse(); "".join(x) Out[5]: ‘987654321’