Reverse a string without using reversed() or [::-1]?

前端 未结 30 2592
南旧
南旧 2020-11-30 19:44

I came across a strange Codecademy exercise that required a function that would take a string as input and return it in reverse order. The only problem was you could not use

30条回答
  •  心在旅途
    2020-11-30 20:16

    i just solved this in code academy and was checking my answers and ran across this list. so with a very limited understanding of python i just did this and it seamed to work.

    def reverse(s):
        i = len(s) - 1
        sNew = ''
        while  i >= 0:
            sNew = sNew + str(s[i])
            i = i -1
        return sNew
    

提交回复
热议问题