可以使用以下方法來實現字符串的反轉:
def reverse_string(s):
return s[::-1]
s = "hello"
reversed_s = reverse_string(s)
print(reversed_s)
def reverse_string(s):
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
return reversed_s
s = "hello"
reversed_s = reverse_string(s)
print(reversed_s)
def reverse_string(s):
if len(s) == 0:
return s
else:
return reverse_string(s[1:]) + s[0]
s = "hello"
reversed_s = reverse_string(s)
print(reversed_s)