初学Python之利用map编写姓名格式化输出函数

  利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。例如输入:['maxbon', 'pythoN', 'aBcdEFGhi'],输出:['Maxbon', 'Python', 'Abcdefghi']

代码:

 1 #定义一个list
 2 L = []
 3 #设置名字的个数
 4 n = int(raw_input("Please enter the number of the name:"))
 5 #利用循环将名字追加到list里面
 6 for num in range(n):
 7     names = raw_input("Please enter the name:")
 8     L.append(names)
 9 #定义函数,首字母大写
10 def format_name(name)
11     return name[0].upper() + name[1:].lower()
12 #利用map()函数输出
13 print map(format_name,L)

输出效果:

1 [root@localhost python]# python format_name.py 
2 Please enter the num of people's name:3
3 Please enter the name:maxbon
4 Please enter the name:pythoN
5 Please enter the name:aBcdEFGhi
6 ['Maxbon', 'Python', 'Abcdefghi']

 

posted @ 2015-06-03 15:34  maxbon  阅读(488)  评论(0)    收藏  举报