How to Reverse a String with Recursive Method in Python


In this example, the length of a string is calculated and the characters are printed out in a reversed order with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a = input("please input your num:")
l = len(a)
print("The number of digits you entered is:",len(a))

def sortDesc(a,l):
    if l == 0:
        return
    print(a[l-1])
    sortDesc(a,l-1)

sortDesc(a,l)
Output:
please input your num: 4321
The number of digits you entered is: 4
1
2
3
4

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments