Reverse a string in Python

前端 未结 28 2485
南旧
南旧 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 04:46

    A lesser perplexing way to look at it would be:

    string = 'happy'
    print(string)
    

    'happy'

    string_reversed = string[-1::-1]
    print(string_reversed)
    

    'yppah'

    In English [-1::-1] reads as:

    "Starting at -1, go all the way, taking steps of -1"

提交回复
热议问题