Day10 函数
- 编写一个函数,交换指定字典的key和value。
例如:dict1={
'a':1, 'b':2, 'c':3} --> dict1={
1:'a', 2:'b', 3:'c'}
def exchange_kv(dict1):
new_dict1 = {
}
for key in dict1:
value = dict1[key]
new_dict1[value] = key
print(new_dict1)
exchange_kv({
'name': 'KathAmy'})
{‘KathAmy’: ‘name’}
- 编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd'
def letter_str(str1):
str2 = ''
for i in str1:
if i.isupper() or i.islower():
str2 += i
print(str2)
letter_str('cdw15%$%^&huhkdjHIJHVGu')
cdwhuhkdjHIJHVGu
- 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
def capitalize1(str1: str):
first_char = str1[0]
if first_char.islower():
return first_char.upper() + str1[1:]
else:
return str1
print(capitalize1('kathAmy'))
KathAmy
- 写一个自己的endswith函数,判断一个字符串是否以指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为