Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.

I have tried lot to reverse the input of 'PYTHON' but I didn't get the output. I also tried the hint given: spell() but it doesn't work out. Can someone help me.

19th Feb 2021, 7:09 AM
Niviedha shekor
Niviedha shekor - avatar
37 Answers
+ 21
def spell(txt): #your code goes here if txt=="": return txt else: print(txt[len(txt)-1]) return spell(txt[0:len(txt)-1]) txt = input() print(spell(txt)) This code is correct for spelling backwards
30th Mar 2021, 7:34 AM
rahul naik
rahul naik - avatar
+ 16
def spell(txt): #your code goes here if txt=="": return else: print(txt[-1]) return spell(txt[:-1]) txt = input() spell(txt)
26th Sep 2021, 5:42 PM
Samir Bettahar
Samir Bettahar - avatar
+ 7
def reverse(text): n = len(text) if n: print(text[-1]) if 1 < n: reverse(text[:-1]) reverse('PYTHON')
19th Feb 2021, 7:17 AM
visph
visph - avatar
+ 5
def spell(txt): #your code goes here while len(txt) > 0: print(txt[-1]) return spell(txt[:-1]) txt = input() spell(txt)
27th Sep 2021, 9:43 AM
Pedro Lozano
Pedro Lozano - avatar
+ 3
def spell(txt): #your code goes here if len(txt)==0: return 1 else: print(txt[(len(txt)-1)]) return spell(txt[0:(len(txt)-1)]) txt = input() spell(txt)
30th Sep 2021, 4:52 PM
jiaqi chen
jiaqi chen - avatar
+ 2
Thanks for your answer dude. But I didn't get the answer. It was given *Complete the recursive spell() function to produce the expected result*
19th Feb 2021, 7:24 AM
Niviedha shekor
Niviedha shekor - avatar
+ 2
def spell(txt): x = len(txt) for a in range(0,x): print(txt[x-1]) x-=1 txt = input() spell(txt)
9th May 2021, 2:32 PM
Davor Budimir
Davor Budimir - avatar
+ 2
Shortest code with recursion : def spell(txt): if len(txt)==1: print(txt) else: print(txt[-1] spell(txt[:-1]) txt = input() spell(txt)
26th Sep 2021, 8:56 PM
Murtada abed
Murtada abed - avatar
+ 2
def spell(txt): #your code goes here if txt == "": return txt else: print(txt[len(txt) - 1]) return spell(txt[0:len(txt) - 1]) txt = input() spell(txt)
24th Oct 2021, 3:39 AM
GURURAJ KL
GURURAJ KL - avatar
+ 2
def spell(txt): #your code goes here if txt=="": return txt else: print(txt[len(txt)-1]) return spell(txt[0:len(txt)-1]) txt = input() print(spell(txt))
9th Dec 2021, 3:00 PM
Mirmahmud Ilyosov
Mirmahmud Ilyosov - avatar
+ 2
def spell(txt): #your code goes here if txt=="": return txt else: print(txt[len(txt)-1]) return spell(txt[0:len(txt)-1]) txt = input() print(spell(txt))
17th Dec 2021, 10:17 AM
Ismoilov Abdug'ofur
Ismoilov Abdug'ofur - avatar
+ 2
def spell(txt): Print(txt[::-1] txt= input() spell(txt)
13th Aug 2022, 1:11 PM
jihad Albush
jihad Albush - avatar
+ 1
def spell(txt): #your code goes here x = len(txt) for a in range(0,x): print(txt[x-1]) x-=1 txt = input() spell(txt)
4th Apr 2021, 2:56 PM
Melhem Kheireddine
Melhem Kheireddine - avatar
+ 1
def spell(txt): #your code goes here if txt == "": return txt else: return txt[::-1] txt = input() print(spell(txt))
24th Aug 2021, 7:31 PM
Leeban J
Leeban J - avatar
+ 1
dude stop using for loops the question is asking for recursion
30th Sep 2021, 4:54 PM
jiaqi chen
jiaqi chen - avatar
+ 1
my_str = (input ()) my_list = list(my_str) my_list2 = my_list [::-1] my_list3 = '\n'.join(my_list2) print(my_list3 ) #simple, no complications
20th Oct 2022, 8:30 PM
Kayode Femi āš”
Kayode Femi āš” - avatar
0
just change 'reverse' by 'spell'
19th Feb 2021, 7:25 AM
visph
visph - avatar
0
#Answer this question def spell(t): X = [] for i in t: X.append(i) r = len(X) -1 while r>-1: print(X[r]) r -= 1 txt = input() spell(txt)
25th Apr 2021, 9:18 PM
Amirhossain Jarareh from IUST University
Amirhossain Jarareh from IUST University - avatar
0
def spell(txt): output = ā€œā€ for i in range(len(txt)): output += txt[(len(txt)-i)-1] + ā€œ\nā€ print(output) An easier way to go about it. This method appends the last element of the word to the output string also adding a new line space. Achieves same output!
17th May 2021, 8:49 PM
Arun Kanwar
Arun Kanwar - avatar
0
But all you have to do it with recursion where is it?
18th Jun 2021, 10:37 AM
Pranav Hirani
Pranav Hirani - avatar