Best way to loop over a python string backwards

后端 未结 11 1918
無奈伤痛
無奈伤痛 2020-12-03 02:16

What is the best way to loop over a python string backwards?

The following seems a little awkward for all the need of -1 offset:

string = \"trick or          


        
11条回答
  •  鱼传尺愫
    2020-12-03 03:07

    Here is a way to reverse a string without utilizing the built in features such as reversed. Negative step values traverse backwards.

    def reverse(text):
        rev = ''
        for i in range(len(text), 0, -1):
            rev += text[i-1]
        return rev
    

提交回复
热议问题