-
编写一个函数,交换指定字典的key和value。
例如:dict1={ 'a':1, 'b':2, 'c':3} --> dict1={ 1:'a', 2:'b', 3:'c'} def exchange(dicts): print({ value: key for key, value in dicts.items()}) exchange({ 'a':1, 'b':2, 'c':3})
-
编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd' #法1 def joint(strs): s = '' for i in strs: if 'a' <= i <= 'z' or 'A' <= i <= 'Z': s += i print(s) joint('12a&bc12d-+') #法2 def get_alphabet(str1): ''' 获取字符串中的字母 :param str1: :return: ''' result = ''.join(x for x in str1 if 'a' <= x <= 'z' or 'A' <= x <= 'Z') # 推导式 + join解决 print(result) get_alphabet('qwersdfsdfwe12344jjk')
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd' # 法1 def capitalize01(strs): if 'a' <= strs[0] <= 'z' or 'A' <= strs[0] <= 'Z': if 'a' <= strs[0] <= 'z': print(strs.replace(strs[0], chr(ord(strs[0]) - 32))) else: print(strs) else: print(strs) capitalize01('abc') # 法2 def capitalize(str1): first = str1[0]
python语言基础+课后练习day10
最新推荐文章于 2023-12-10 17:05:49 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)