Reverse a string in Python

前端 未结 28 2473
南旧
南旧 2020-11-21 04:41

There is no built in reverse function for Python\'s str object. What is the best way of implementing this method?

If supplying a very conci

28条回答
  •  误落风尘
    2020-11-21 05:05

    How about:

    >>> 'hello world'[::-1]
    'dlrow olleh'
    

    This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.

提交回复
热议问题