1、
capitalize()
将字符串的首字母大写,其余字母小写
2、title()
将字符串中每个单词的首字母大写,其余字母小写,非字母后的第一个字母将转换为大写字母
3、upper()
&lower()
全部单词大写或小写
def normalize1(name):
# capitalize()将字符串的第一个字母变成大写,其他字母变小写。
return name.capitalize()
def normalize2(name):
# title() 方法返回"标题化"的字符串,将字符串中每个单词的首个字母转化为大写,其余字母均为小写
# 非字母后的第一个字母将转换为大写字母
return name.title()
def normalize3(name):
return name[0].upper()+name[1:].lower()
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize1, L1))
print(L2)
输出结果相同为:
[‘Adam’, ‘Lisa’, ‘Bart’]
注意差异
1、capitalize()
str1="this is a string."
str1.capitalize()
‘This is a string.’
注意:第一个是非字母时,非字母后的第一个字母也不会大写
str2="2this is a string."
str2.capitalize()
‘2this is a string.’
2、title()
str1="this is a string."
str1.title()
‘This Is A String.’
注意:非字母后的第一个字母都将转换为大写字母
str2='i love 4g4g4g4g'
str2.title()
‘I Love 4G4G4G4G’
3、upper() & lower()
str1="this is a string."
str1.upper()
‘THIS IS A STRING.’
str1.upper().lower()
‘this is a string.’