python之字符串反转
1.反向循环迭代
a ='abcdefg' b = '' for i in a: b = i+b print(b)
2.反转列表
将字符串转换为列表,反转列表,将列表转换为字符串
a = 'abcdefg' b = list(a) b.reverse() a = ''.join(b) print(a)
3.反向循环迭代
a = 'abcdefg' b = '' for i in a[::-1]: b += i print(b)
4.列表弹出发
a = 'abcdefg' a = list(a) b = '' while len(a) > 0: b += a.pop() print(b)