python 字符串反转 1. python 字符转切片实现
python代码
name = tramp
print name[::-1]
2. python 递归实现
python代码
def reverse(astr):
“””reverse a string”””
if len(astr) == 0:
return astr
newstr = reverse(astr[1:]) + astr[0]
return newstr
A programmer’s note boke for work and life
python 字符串反转 1. python 字符转切片实现
python代码
name = tramp
print name[::-1]
2. python 递归实现
python代码
def reverse(astr):
“””reverse a string”””
if len(astr) == 0:
return astr
newstr = reverse(astr[1:]) + astr[0]
return newstr