#第一种:使用字符串切片 result = s[::-1] #第二种:使用列表的reverse方法 l = list(s) l.reverse() result = "".join(l) #当然下面也行 l = list(s) result = "".join(l[::-1]) #第三种:使用reduce result = reduce(lambda x,y:y+x,s) #第四种:使用递归函数 def func(s): if len(s) <1: return s return func(s[1:])+s[0] result = func(s) #第五种:使用栈 def func(s): l = list(s) #模拟全部入栈 result = "" while len(l)>0: result += l.pop() #模拟出栈 return result result = func(s) #第六种:for循环 def func(s): result = "" max_index = len(s)-1 for index,value in enumerate(s): result += s[max_index-index] return result result = func(s) #第七种:while循环 def func(s): ret = ‘‘ len_ = len(s) -1 while len_ >= 0 : ret += s[len_] len_ = len_ -1 #print (ret) return ret
转于:https://www.cnblogs.com/taceywong/p/8045127.html 并添加
原文地址:https://www.cnblogs.com/fanbi/p/11782795.html
时间: 2024-11-14 08:07:48