1. 程式人生 > >Python中遞迴字串反轉

Python中遞迴字串反轉

我們可以用Python輕鬆的反轉字串,程式碼如下:

def reverse(s):
    if s == '':
        return s
    else:
        return reverse(s[1:]) + s[0]

print(reverse('hello'))

執行結果如下:
Connected to pydev debugger (build 143.1559)
olleh

Process finished with exit code 0