Maison > Article > développement back-end > Python 字符串反转
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