字符串反转的几种方法
def reverse_str(s): from functools import reduce res = s[::-1] # 切片 res = "".join(list(reversed(s))) # 反转函数 res = reduce(lambda x,y:y+x, s) # reduce print(res) def recursion_reverse_str(s): # 递归 if len(s) < 1: return "" return recursion_reverse_str(s[1:]) + s[0] def stack_reverse_str(s): # 栈 li = list(s) res = "" while len(li)>0: res += li.pop() return res def loop_str(s): # 循环 res = "" for i in range(len(s)-1, -1, -1): res += s[i] return res
时刻记着自己要成为什么样的人!