码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-13 13:08:01      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:map   python   

   利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。例如输入:[‘adam‘, ‘LISA‘, ‘barT‘],输出:[‘Adam‘, ‘Lisa‘, ‘Bart‘]

    代码:

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

   输出效果:

[root@localhost python]# python format_name.py 
Please enter the num of people‘s name:3
Please enter the name:maxbon
Please enter the name:pythoN
Please enter the name:aBcdEFGhi
[‘Maxbon‘, ‘Python‘, ‘Abcdefghi‘]


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

标签:map   python   

原文地址:http://maxbon.blog.51cto.com/6304762/1650876

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!