Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 27

Python Exercise Example 27

Python 100 Examples Python 100 Examples

**Problem:** Use recursive function calls to print the 5 input characters in reverse order.

**Analysis:** None.

Program source code:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

def

output

(

s

,

l

)

:

if

l

==

0

:

return

print

(

s

[

l

1

]

)

output

(

s

,

l

1

)

s

=

raw_input

(

'

Input a string:

'

)

l

=

len

(

s

)

output

(

s

,

l

)

Example (Python 3.0+)

#!/usr/bin/python3

def

output

(

s

,

l

)

:

if

l

==

0

:

return

print

(

s

[

l

1

]

)

output

(

s

,

l

1

)

s

=

input

(

'

Input a string:

'

)

l

=

len

(

s

)

output

(

s

,

l

)

The output of the above example is:


Input a string:abcde
e
d
c
b
a

Python 100 Examples Python 100 Examples