Fastest way to reverse a string in python

我是研究僧i 提交于 2019-12-01 02:20:15

I see a difference.

First of all, what is up with map(lambda x: x, st)? What is the purpose?

Use the timeit module to test your code:

$ python -m timeit '"".join(reversed("abcdefghijklmnopqrstuvwxyz"))'
1000000 loops, best of 3: 0.586 usec per loop
$ python -m timeit '"abcdefghijklmnopqrstuvwxyz"[::-1]'           
10000000 loops, best of 3: 0.0715 usec per loop

As you can see, the slice is ~8x faster on my machine for this particular input. It's also more concise.

s=input("enter string")
print(s[::-1])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!