indexing to reverse the string in python code example
Example 1: python reverse a string
#linear
def reverse(s):
str = ""
for i in s:
str = i + str
return str
#splicing
'hello world'[::-1]
Example 2: Python reverse a string
# Library
def solution(str):
return ''.join(reversed(str))
# DIY with recursion
def solution(str):
if len(str) == 0:
return str
else:
return solution(str[1:]) + str[0]