利用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‘]
原文地址:http://maxbon.blog.51cto.com/6304762/1650876