Home  >  Article  >  Backend Development  >  Python string reverse

Python string reverse

巴扎黑
巴扎黑Original
2016-12-07 10:23:461308browse

Python string reversal
1. Python character to slice implementation

Python code

name = tramp  
print name[::-1]



2. Python recursive implementation

Python code

def reverse(aStr):  
    """reverse a string"""  
    if len(aStr) == 0:  
        return aStr  
    newStr = reverse(aStr[1:]) + aStr[0]  
    return newStr


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:python list sortingNext article:python list sorting