s='dsdsfsdsf'
"""使用字符串切片"""
result=s[::-1]
print(result)
"""使用列表的reverse"""
l=list(s)
result=list(reversed(l))
print(result)
"""使用reduce"""
from functools import reduce
result=reduce(lambda x,y:y+x,s)
print(result)
"""使用栈"""
def func(s):
l = list(s) #模拟全部入栈
result = ""
while len(l)>0:
result += l.pop() #模拟出栈
return result
result = func(s)
print(result)
结果:
F:\pythonspace\venv\Scripts\python.exe F:/pythonspace/反转字符串.py
fsdsfsdsd
['f', 's', 'd', 's', 'f', 's', 'd', 's', 'd']
fsdsfsdsd
fsdsfsdsd
Process finished with exit code 0