def reverseStr(s):
#解法1
lists = list(s)
left = 0
right = len(s) - 1
while left < right:
lists[left], lists[right] = lists[right], lists[left]
left += 1
right -= 1
s = ''.join(lists)
return s
#解法2
#return [::-1]
#解法3
from functools import reduceStr
def reverseStr(str1):
return reduce(lambda x,y:y+x,str1)
str1 = 'sdfge1'
a = reverseStr(str1)
print(a)
但是当字符串足够大时,解法三时间复杂度过高,不建议使用