If there is a string , which must be reversed , for example :
reverse of '' is '' reverse of 'a' is 'a' reverse of 'ab' is 'ba'
In python this can be done like this :
>>> ''.join(reversed('ab'))
'ba'
>>> ''.join(reversed('abcdef'))
'fedcba'
The reversed class , and the join string method , were used . The reversed class , returns a reverse iterator , and the
join method , joins the reverse iterator data , to the empty string .
Another way to reverse a string in python , is to create a recursive function .
def reverseString(string: str):
'''reverse a string recursively
@param string : str , a string to reverse
'''
if len(string) == 0:
return ''
if len(string) == 1:
return string[0]
else:
return string[-1] + reverseString(string[1:-1]) + string[0]
# take two characters at a time
# reverse them , and call the reverseString
# method on the remaining characters
>>> print(reverseString(''))
>>> print(reverseString('a'))
a
>>> print(reverseString('ab'))
ba
>>> print(reverseString('abc'))
cba

