Discuss / Python / 作业代码--把名字规范化成首字母大写,其他小写

作业代码--把名字规范化成首字母大写,其他小写

Topic source

sofa_th

#1 Created at ... [Delete] [Delete and Lock User]
LP=['adam', 'LISA', 'barT']
def lowerChr(s):
	t=ord(s)
	if t>64 and t<91:
		return chr(t+32)
	elif t>96 and t<123:
		return s
	else:
		return 'not a char'

def capitalWord(w):
	lw=list(map(lowerChr,w))
	lw[0]=chr(ord(lw[0])-32)
	s=''
	for i in range(len(lw)):
		s=s+lw[i]
	return s	
print(capitalWord('LISA'))
print(list(map(capitalWord,LP)))

sofa_th

#2 Created at ... [Delete] [Delete and Lock User]

看下面评论里有直接使用 转换大小写和首字母大写的函数,经过测试,修改作业代码:

def sName(name):

return name.capitalize()

print list(map(sName,['adam', 'LISA', 'barT']))

>>> 'ABC'.capitalize()

'Abc'

>>> 'ABC'.lower()

'abc'

>>> 'abc'.upper()

'ABC'

金钟铉

#3 Created at ... [Delete] [Delete and Lock User]

兄弟一楼那个好六啊

def normalize(name):    return name.capitalize()# 测试:L1 = ['adam', 'LISA', 'barT']L2 = list(map(normalize, L1))print(L2)

  • 1

Reply