Home » Python recursive function to print a string backwards

Python recursive function to print a string backwards

Recursive function to print a string backwards

def strback(str,n):
    if n>0:
        k=len(str)-n
        strback(str,n-1)
        print(str[k],end=' ')
    elif n==0:
        return


# __main__
s=input("Enter a string:")
strback(s,len(s))

Contents

Output:

Enter a string: computer science
e c n e i c s r e t u p m o c