Python 练习实例27
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
程序分析:无。
程序源代码:
#!/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)
以上实例输出结果为:
Input a string:abcde e d c b a
Python 100例
微笑依旧
dzhangkai@163.com
使用负数下标:
#!/usr/bin/python # -*- coding: UTF-8 -*- def desc_output(s): if(len(s) > 0): print(s[-1]) # python 负数下标 desc_output(s[0:-1]) s = raw_input('Input a string:') desc_output(s)微笑依旧
dzhangkai@163.com
等一个人
252343465@qq.com
Python3 下非递归,使用各列表的 reverse() 方法:
#!/usr/bin/env python3 S = input('Input a string:') L = list(S) L.reverse() for i in range(len(L)): print(L[i])等一个人
252343465@qq.com