1.将一个字符串倒序
def str_reverse(s):
return s[::-1]
def func2(s):
list1 = list(s)
list1.reverse()
return ''.join(list1)
def func3(s):
list1 = []
for i in range(len(s)-1,-1,-1):
list1.append(s[i])
return ''.join(list1)
def func4(s):
if len(s)<=1:
return s
return s[-1] + func4(s[:-1])
def rever(st):
b = len(st) >> 1
d = list(st)
c = len(st)
for i in range(b):
d[i], d[c - 1 - i] = d[c - 1 - i], d[i]
return ''.join(d)
a = '123321'
b = '12321'
print(rever(a)) # 123321
print(rever(b)) # 12321
网友评论