tseew 发表于 2015-5-14 08:47:26

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
#定义一个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.upper() + name.lower()
#利用map()函数输出
print map(format_name,L)




    输出效果:


1
2
3
4
5
6
# 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']



页: [1]
查看完整版本: 初学Python之利用map编写姓名格式化输出函数