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

前端 未结 30 2580
南旧
南旧 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:06

    Only been coding Python for a few days, but I feel like this was a fairly clean solution. Create an empty list, loop through each letter in the string and append it to the front of the list, return the joined list as a string.

    def reverse(text):
    backwardstext = []
    for letter in text:
        backwardstext.insert(0, letter)
    return ''.join(backwardstext)
    

提交回复
热议问题