test_str = "python is ok."
print(test_str[::-1])
def reverse_str(s):
if s == "":
return s
else:
return reverse_str(s[1:]) + s[0]
test_str = "python is ok."
print(reverse_str(test_str))
test_str = "python is ok."
s = list(test_str)
s.reverse()
test_str = "".join(s)
print(test_str)
from functools import reduce
test_str = "python is ok."
print(reduce(lambda x, y: y+x, test_str))
#help(reduce)
def rever(s):
ret = ""
for i in range(len(s)-1, -1, -1):
ret += s[i]
return ret
test_str = "python is ok."
print(rever(test_str))
def rev(s):
lst = list(s) # 转换成list
ret = ""
while len(lst):
ret += lst.pop() # 每次弹出最后的元素
return ret
test_str = "python is ok."
print(rev(test_str))
原文:https://www.cnblogs.com/sinlearn/p/13061229.html